Solution #7d579923-0e58-45f4-8271-f49fbd5b2fc4
completedScore
100% (5/5)
Runtime
33μs
Delta
No change vs parent
Tied for best
Same as parent
Score
100% (5/5)
Runtime
33μ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 = []
append = jobs.append
for j in raw:
if isinstance(j, (list, tuple)) and len(j) == 3:
append((j[1], j[0], j[2])) # (end, start, weight)
if not jobs:
return 0
jobs.sort()
import bisect
frontier_ends = [0]
frontier_vals = [0]
for e, s, w in jobs:
idx = bisect.bisect_right(frontier_ends, s) - 1
cand = frontier_vals[idx] + w
if cand <= frontier_vals[-1]:
continue
pos = bisect.bisect_left(frontier_ends, e)
if pos < len(frontier_ends) and frontier_ends[pos] == e:
if cand <= frontier_vals[pos]:
continue
frontier_vals[pos] = cand
pos += 1
else:
frontier_ends.insert(pos, e)
frontier_vals.insert(pos, cand)
pos += 1
while pos < len(frontier_vals) and frontier_vals[pos] <= cand:
del frontier_vals[pos]
del frontier_ends[pos]
return frontier_vals[-1]Score Difference
Tied
Runtime Advantage
15μs slower
Code Size
46 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 | append = jobs.append | 9 | for j in raw: |
| 10 | for j in raw: | 10 | if isinstance(j, (list, tuple)) and len(j) == 3: |
| 11 | 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 | append((j[1], j[0], j[2])) # (end, start, weight) | 12 | if not jobs: |
| 13 | 13 | return 0 | |
| 14 | if not jobs: | 14 | |
| 15 | return 0 | 15 | jobs.sort() |
| 16 | 16 | ends = [] | |
| 17 | jobs.sort() | 17 | bests = [] |
| 18 | 18 | ||
| 19 | import bisect | 19 | for e, s, w in jobs: |
| 20 | 20 | lo = 0 | |
| 21 | frontier_ends = [0] | 21 | hi = len(ends) |
| 22 | frontier_vals = [0] | 22 | while lo < hi: |
| 23 | 23 | mid = (lo + hi) >> 1 | |
| 24 | for e, s, w in jobs: | 24 | if ends[mid] <= s: |
| 25 | idx = bisect.bisect_right(frontier_ends, s) - 1 | 25 | lo = mid + 1 |
| 26 | cand = frontier_vals[idx] + w | 26 | else: |
| 27 | 27 | hi = mid | |
| 28 | if cand <= frontier_vals[-1]: | 28 | |
| 29 | continue | 29 | candidate = w + (bests[lo - 1] if lo else 0) |
| 30 | 30 | current = bests[-1] if bests else 0 | |
| 31 | pos = bisect.bisect_left(frontier_ends, e) | 31 | |
| 32 | if pos < len(frontier_ends) and frontier_ends[pos] == e: | 32 | if candidate > current: |
| 33 | if cand <= frontier_vals[pos]: | 33 | if ends and ends[-1] == e: |
| 34 | continue | 34 | bests[-1] = candidate |
| 35 | frontier_vals[pos] = cand | 35 | else: |
| 36 | pos += 1 | 36 | ends.append(e) |
| 37 | else: | 37 | bests.append(candidate) |
| 38 | frontier_ends.insert(pos, e) | 38 | |
| 39 | frontier_vals.insert(pos, cand) | 39 | return bests[-1] if bests else 0 |
| 40 | pos += 1 | 40 | |
| 41 | 41 | ||
| 42 | while pos < len(frontier_vals) and frontier_vals[pos] <= cand: | 42 | |
| 43 | del frontier_vals[pos] | 43 | |
| 44 | del frontier_ends[pos] | 44 | |
| 45 | 45 | ||
| 46 | return frontier_vals[-1] | 46 |
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 append = jobs.append10 for j in raw:11 if isinstance(j, (list, tuple)) and len(j) == 3:12 append((j[1], j[0], j[2])) # (end, start, weight)1314 if not jobs:15 return 01617 jobs.sort()1819 import bisect2021 frontier_ends = [0]22 frontier_vals = [0]2324 for e, s, w in jobs:25 idx = bisect.bisect_right(frontier_ends, s) - 126 cand = frontier_vals[idx] + w2728 if cand <= frontier_vals[-1]:29 continue3031 pos = bisect.bisect_left(frontier_ends, e)32 if pos < len(frontier_ends) and frontier_ends[pos] == e:33 if cand <= frontier_vals[pos]:34 continue35 frontier_vals[pos] = cand36 pos += 137 else:38 frontier_ends.insert(pos, e)39 frontier_vals.insert(pos, cand)40 pos += 14142 while pos < len(frontier_vals) and frontier_vals[pos] <= cand:43 del frontier_vals[pos]44 del frontier_ends[pos]4546 return frontier_vals[-1]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])) # 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