Solution #14fae373-b5ae-42c4-a7be-27c81b954e29
completedScore
100% (5/5)
Runtime
25μs
Delta
+400.0% vs parent
Tied for best
Improved from parent
Score
100% (5/5)
Runtime
25μs
Delta
+400.0% vs parent
Tied for best
Improved from 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])
frontier = [(float("-inf"), 0)] # (end_time, best_weight_up_to_end)
for s, e, w in jobs:
lo = 0
hi = len(frontier)
while lo < hi:
mid = (lo + hi) >> 1
if frontier[mid][0] <= s:
lo = mid + 1
else:
hi = mid
base = frontier[lo - 1][1]
cand = base + w
if cand <= frontier[-1][1]:
continue
if e == frontier[-1][0]:
frontier[-1] = (e, cand)
else:
frontier.append((e, cand))
return frontier[-1][1]Score Difference
Tied
Runtime Advantage
7μs slower
Code Size
39 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 | frontier = [(float("-inf"), 0)] # (end_time, best_weight_up_to_end) | 17 | bests = [] |
| 18 | 18 | ||
| 19 | for s, e, w in jobs: | 19 | for e, s, w in jobs: |
| 20 | lo = 0 | 20 | lo = 0 |
| 21 | hi = len(frontier) | 21 | hi = len(ends) |
| 22 | while lo < hi: | 22 | while lo < hi: |
| 23 | mid = (lo + hi) >> 1 | 23 | mid = (lo + hi) >> 1 |
| 24 | if frontier[mid][0] <= s: | 24 | if ends[mid] <= s: |
| 25 | lo = mid + 1 | 25 | lo = mid + 1 |
| 26 | else: | 26 | else: |
| 27 | hi = mid | 27 | hi = mid |
| 28 | base = frontier[lo - 1][1] | 28 | |
| 29 | cand = base + w | 29 | candidate = w + (bests[lo - 1] if lo else 0) |
| 30 | 30 | current = bests[-1] if bests else 0 | |
| 31 | if cand <= frontier[-1][1]: | 31 | |
| 32 | continue | 32 | if candidate > current: |
| 33 | 33 | if ends and ends[-1] == e: | |
| 34 | if e == frontier[-1][0]: | 34 | bests[-1] = candidate |
| 35 | frontier[-1] = (e, cand) | 35 | else: |
| 36 | else: | 36 | ends.append(e) |
| 37 | frontier.append((e, cand)) | 37 | bests.append(candidate) |
| 38 | 38 | ||
| 39 | return frontier[-1][1] | 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[0], j[1], j[2]))12 if not jobs:13 return 01415 jobs.sort(key=lambda x: x[1])1617 frontier = [(float("-inf"), 0)] # (end_time, best_weight_up_to_end)1819 for s, e, w in jobs:20 lo = 021 hi = len(frontier)22 while lo < hi:23 mid = (lo + hi) >> 124 if frontier[mid][0] <= s:25 lo = mid + 126 else:27 hi = mid28 base = frontier[lo - 1][1]29 cand = base + w3031 if cand <= frontier[-1][1]:32 continue3334 if e == frontier[-1][0]:35 frontier[-1] = (e, cand)36 else:37 frontier.append((e, cand))3839 return frontier[-1][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