Solution #a9989ddf-16c2-4cb2-b27f-ba100d1f21f3
completedScore
100% (5/5)
Runtime
23μs
Delta
No change vs parent
Tied for best
Same as parent
Score
100% (5/5)
Runtime
23μs
Delta
No change vs parent
Tied for best
Same as parent
def solve(input):
if not isinstance(input, dict):
return 0
raw = input.get("jobs")
if not raw:
return 0
jobs = []
for j in raw:
if isinstance(j, (list, tuple)) and len(j) == 3:
jobs.append((j[1], j[0], j[2])) # (end, start, weight)
if not jobs:
return 0
jobs.sort()
from bisect import bisect_right
frontier_ends = []
frontier_vals = []
for end, start, weight in jobs:
i = bisect_right(frontier_ends, start) - 1
base = frontier_vals[i] if i >= 0 else 0
cand = base + weight
if frontier_vals and cand <= frontier_vals[-1]:
continue
j = bisect_right(frontier_ends, end)
if j < len(frontier_ends):
del frontier_ends[j:]
del frontier_vals[j:]
frontier_ends.append(end)
frontier_vals.append(cand)
return frontier_vals[-1] if frontier_vals else 0Score Difference
Tied
Runtime Advantage
5μs slower
Code Size
38 vs 39 lines
| # | Your Solution | # | Champion |
|---|---|---|---|
| 1 | def solve(input): | 1 | def solve(input): |
| 2 | if not isinstance(input, dict): | 2 | if not isinstance(input, dict): |
| 3 | return 0 | 3 | return 0 |
| 4 | raw = input.get("jobs") | 4 | raw = input.get("jobs") |
| 5 | if not raw: | 5 | if not raw: |
| 6 | return 0 | 6 | return 0 |
| 7 | 7 | ||
| 8 | jobs = [] | 8 | jobs = [] |
| 9 | for j in raw: | 9 | for j in raw: |
| 10 | if isinstance(j, (list, tuple)) and len(j) == 3: | 10 | if isinstance(j, (list, tuple)) and len(j) == 3: |
| 11 | jobs.append((j[1], j[0], j[2])) # (end, start, weight) | 11 | jobs.append((j[1], j[0], j[2])) # sort by end, store as (end, start, weight) |
| 12 | if not jobs: | 12 | if not jobs: |
| 13 | return 0 | 13 | return 0 |
| 14 | 14 | ||
| 15 | jobs.sort() | 15 | jobs.sort() |
| 16 | 16 | ends = [] | |
| 17 | from bisect import bisect_right | 17 | bests = [] |
| 18 | 18 | ||
| 19 | frontier_ends = [] | 19 | for e, s, w in jobs: |
| 20 | frontier_vals = [] | 20 | lo = 0 |
| 21 | 21 | hi = len(ends) | |
| 22 | for end, start, weight in jobs: | 22 | while lo < hi: |
| 23 | i = bisect_right(frontier_ends, start) - 1 | 23 | mid = (lo + hi) >> 1 |
| 24 | base = frontier_vals[i] if i >= 0 else 0 | 24 | if ends[mid] <= s: |
| 25 | cand = base + weight | 25 | lo = mid + 1 |
| 26 | 26 | else: | |
| 27 | if frontier_vals and cand <= frontier_vals[-1]: | 27 | hi = mid |
| 28 | continue | 28 | |
| 29 | 29 | candidate = w + (bests[lo - 1] if lo else 0) | |
| 30 | j = bisect_right(frontier_ends, end) | 30 | current = bests[-1] if bests else 0 |
| 31 | if j < len(frontier_ends): | 31 | |
| 32 | del frontier_ends[j:] | 32 | if candidate > current: |
| 33 | del frontier_vals[j:] | 33 | if ends and ends[-1] == e: |
| 34 | 34 | bests[-1] = candidate | |
| 35 | frontier_ends.append(end) | 35 | else: |
| 36 | frontier_vals.append(cand) | 36 | ends.append(e) |
| 37 | 37 | bests.append(candidate) | |
| 38 | return frontier_vals[-1] if frontier_vals else 0 | 38 | |
| 39 | 39 | return bests[-1] if bests else 0 |
1def solve(input):2 if not isinstance(input, dict):3 return 04 raw = input.get("jobs")5 if not raw:6 return 078 jobs = []9 for j in raw:10 if isinstance(j, (list, tuple)) and len(j) == 3:11 jobs.append((j[1], j[0], j[2])) # (end, start, weight)12 if not jobs:13 return 01415 jobs.sort()1617 from bisect import bisect_right1819 frontier_ends = []20 frontier_vals = []2122 for end, start, weight in jobs:23 i = bisect_right(frontier_ends, start) - 124 base = frontier_vals[i] if i >= 0 else 025 cand = base + weight2627 if frontier_vals and cand <= frontier_vals[-1]:28 continue2930 j = bisect_right(frontier_ends, end)31 if j < len(frontier_ends):32 del frontier_ends[j:]33 del frontier_vals[j:]3435 frontier_ends.append(end)36 frontier_vals.append(cand)3738 return frontier_vals[-1] if frontier_vals else 01def solve(input):2 if not isinstance(input, dict):3 return 04 raw = input.get("jobs")5 if not raw:6 return 078 jobs = []9 for j in raw:10 if isinstance(j, (list, tuple)) and len(j) == 3:11 jobs.append((j[1], j[0], j[2])) # sort by end, store as (end, start, weight)12 if not jobs:13 return 01415 jobs.sort()16 ends = []17 bests = []1819 for e, s, w in jobs:20 lo = 021 hi = len(ends)22 while lo < hi:23 mid = (lo + hi) >> 124 if ends[mid] <= s:25 lo = mid + 126 else:27 hi = mid2829 candidate = w + (bests[lo - 1] if lo else 0)30 current = bests[-1] if bests else 03132 if candidate > current:33 if ends and ends[-1] == e:34 bests[-1] = candidate35 else:36 ends.append(e)37 bests.append(candidate)3839 return bests[-1] if bests else 0