Solution #b9b05796-90a9-40e0-9055-4e85956ba038
completedScore
100% (5/5)
Runtime
57μs
Delta
No change vs parent
Tied for best
Same as parent
Score
100% (5/5)
Runtime
57μ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[0], j[1], j[2]))
if not jobs:
return 0
jobs.sort(key=lambda x: x[1])
best_ends = [jobs[0][1]]
best_vals = [max(0, jobs[0][2])]
for i in range(1, len(jobs)):
s, e, w = jobs[i]
lo, hi = 0, len(best_ends)
while lo < hi:
mid = (lo + hi) >> 1
if best_ends[mid] <= s:
lo = mid + 1
else:
hi = mid
prev_best = best_vals[lo - 1] if lo else 0
cand = prev_best + w
cur_best = best_vals[-1]
if cand > cur_best:
if e == best_ends[-1]:
best_vals[-1] = cand
else:
best_ends.append(e)
best_vals.append(cand)
import functools
@functools.lru_cache(None)
def ans(i):
return best_vals[i]
return ans(len(best_vals) - 1) if best_vals else 0Score Difference
Tied
Runtime Advantage
39μ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 | 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[0], j[1], j[2])) | 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(key=lambda x: x[1]) | 15 | jobs.sort() |
| 16 | 16 | ends = [] | |
| 17 | best_ends = [jobs[0][1]] | 17 | bests = [] |
| 18 | best_vals = [max(0, jobs[0][2])] | 18 | |
| 19 | 19 | for e, s, w in jobs: | |
| 20 | for i in range(1, len(jobs)): | 20 | lo = 0 |
| 21 | s, e, w = jobs[i] | 21 | hi = len(ends) |
| 22 | 22 | while lo < hi: | |
| 23 | lo, hi = 0, len(best_ends) | 23 | mid = (lo + hi) >> 1 |
| 24 | while lo < hi: | 24 | if ends[mid] <= s: |
| 25 | mid = (lo + hi) >> 1 | 25 | lo = mid + 1 |
| 26 | if best_ends[mid] <= s: | 26 | else: |
| 27 | lo = mid + 1 | 27 | hi = mid |
| 28 | else: | 28 | |
| 29 | hi = mid | 29 | candidate = w + (bests[lo - 1] if lo else 0) |
| 30 | prev_best = best_vals[lo - 1] if lo else 0 | 30 | current = bests[-1] if bests else 0 |
| 31 | cand = prev_best + w | 31 | |
| 32 | cur_best = best_vals[-1] | 32 | if candidate > current: |
| 33 | if cand > cur_best: | 33 | if ends and ends[-1] == e: |
| 34 | if e == best_ends[-1]: | 34 | bests[-1] = candidate |
| 35 | best_vals[-1] = cand | 35 | else: |
| 36 | else: | 36 | ends.append(e) |
| 37 | best_ends.append(e) | 37 | bests.append(candidate) |
| 38 | best_vals.append(cand) | 38 | |
| 39 | 39 | return bests[-1] if bests else 0 | |
| 40 | import functools | 40 | |
| 41 | 41 | ||
| 42 | @functools.lru_cache(None) | 42 | |
| 43 | def ans(i): | 43 | |
| 44 | return best_vals[i] | 44 | |
| 45 | 45 | ||
| 46 | return ans(len(best_vals) - 1) if best_vals else 0 | 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 for j in raw:10 if isinstance(j, (list, tuple)) and len(j) == 3:11 jobs.append((j[0], j[1], j[2]))12 if not jobs:13 return 01415 jobs.sort(key=lambda x: x[1])1617 best_ends = [jobs[0][1]]18 best_vals = [max(0, jobs[0][2])]1920 for i in range(1, len(jobs)):21 s, e, w = jobs[i]2223 lo, hi = 0, len(best_ends)24 while lo < hi:25 mid = (lo + hi) >> 126 if best_ends[mid] <= s:27 lo = mid + 128 else:29 hi = mid30 prev_best = best_vals[lo - 1] if lo else 031 cand = prev_best + w32 cur_best = best_vals[-1]33 if cand > cur_best:34 if e == best_ends[-1]:35 best_vals[-1] = cand36 else:37 best_ends.append(e)38 best_vals.append(cand)3940 import functools4142 @functools.lru_cache(None)43 def ans(i):44 return best_vals[i]4546 return ans(len(best_vals) - 1) if best_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