Solution #82526443-92a4-468f-983c-3d3ffbe48d15
completedScore
80% (4/5)
Runtime
15μs
Delta
No change vs parent
-20.0% vs best
Same as parent
Score
80% (4/5)
Runtime
15μs
Delta
No change vs parent
-20.0% vs best
Same as parent
def solve(input):
jobs = input.get("jobs", [])
if not jobs:
return 0
jobs = sorted(jobs, key=lambda x: x[1])
best_end = [0]
best_val = [0]
for s, e, w in jobs:
lo, hi = 0, len(best_end) - 1
idx = 0
while lo <= hi:
mid = (lo + hi) // 2
if best_end[mid] <= s:
idx = mid
lo = mid + 1
else:
hi = mid - 1
cand = best_val[idx] + w
if cand > best_val[-1]:
if best_end[-1] == e:
best_val[-1] = cand
else:
best_end.append(e)
best_val.append(cand)
return best_val[-1]Score Difference
-20.0%
Runtime Advantage
3μs faster
Code Size
30 vs 39 lines
| # | Your Solution | # | Champion |
|---|---|---|---|
| 1 | def solve(input): | 1 | def solve(input): |
| 2 | jobs = input.get("jobs", []) | 2 | if not isinstance(input, dict): |
| 3 | if not jobs: | 3 | return 0 |
| 4 | return 0 | 4 | raw = input.get("jobs") |
| 5 | 5 | if not raw: | |
| 6 | jobs = sorted(jobs, key=lambda x: x[1]) | 6 | return 0 |
| 7 | 7 | ||
| 8 | best_end = [0] | 8 | jobs = [] |
| 9 | best_val = [0] | 9 | for j in raw: |
| 10 | 10 | if isinstance(j, (list, tuple)) and len(j) == 3: | |
| 11 | for s, e, w in jobs: | 11 | jobs.append((j[1], j[0], j[2])) # sort by end, store as (end, start, weight) |
| 12 | lo, hi = 0, len(best_end) - 1 | 12 | if not jobs: |
| 13 | idx = 0 | 13 | return 0 |
| 14 | while lo <= hi: | 14 | |
| 15 | mid = (lo + hi) // 2 | 15 | jobs.sort() |
| 16 | if best_end[mid] <= s: | 16 | ends = [] |
| 17 | idx = mid | 17 | bests = [] |
| 18 | lo = mid + 1 | 18 | |
| 19 | else: | 19 | for e, s, w in jobs: |
| 20 | hi = mid - 1 | 20 | lo = 0 |
| 21 | 21 | hi = len(ends) | |
| 22 | cand = best_val[idx] + w | 22 | while lo < hi: |
| 23 | if cand > best_val[-1]: | 23 | mid = (lo + hi) >> 1 |
| 24 | if best_end[-1] == e: | 24 | if ends[mid] <= s: |
| 25 | best_val[-1] = cand | 25 | lo = mid + 1 |
| 26 | else: | 26 | else: |
| 27 | best_end.append(e) | 27 | hi = mid |
| 28 | best_val.append(cand) | 28 | |
| 29 | 29 | candidate = w + (bests[lo - 1] if lo else 0) | |
| 30 | return best_val[-1] | 30 | current = bests[-1] if bests else 0 |
| 31 | 31 | ||
| 32 | 32 | if candidate > current: | |
| 33 | 33 | if ends and ends[-1] == e: | |
| 34 | 34 | bests[-1] = candidate | |
| 35 | 35 | else: | |
| 36 | 36 | ends.append(e) | |
| 37 | 37 | bests.append(candidate) | |
| 38 | 38 | ||
| 39 | 39 | return bests[-1] if bests else 0 |
1def solve(input):2 jobs = input.get("jobs", [])3 if not jobs:4 return 056 jobs = sorted(jobs, key=lambda x: x[1])78 best_end = [0]9 best_val = [0]1011 for s, e, w in jobs:12 lo, hi = 0, len(best_end) - 113 idx = 014 while lo <= hi:15 mid = (lo + hi) // 216 if best_end[mid] <= s:17 idx = mid18 lo = mid + 119 else:20 hi = mid - 12122 cand = best_val[idx] + w23 if cand > best_val[-1]:24 if best_end[-1] == e:25 best_val[-1] = cand26 else:27 best_end.append(e)28 best_val.append(cand)2930 return best_val[-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