Solution #eef58d00-d96c-4441-9077-ef477d9f9264
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 job in raw:
if isinstance(job, (list, tuple)) and len(job) == 3:
s, e, w = job
jobs.append((s, e, w))
if not jobs:
return 0
jobs.sort(key=lambda x: (x[1], x[0], x[2]))
states_end = [float("-inf")]
states_val = [0]
for s, e, w in jobs:
lo, hi = 0, len(states_end)
while lo < hi:
mid = (lo + hi) // 2
if states_end[mid] <= s:
lo = mid + 1
else:
hi = mid
base = states_val[lo - 1]
cand = base + w
if cand <= states_val[-1]:
continue
if e == states_end[-1]:
states_val[-1] = cand
continue
states_end.append(e)
states_val.append(cand)
return states_val[-1]Score Difference
Tied
Runtime Advantage
7μs slower
Code Size
43 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 job in raw: | 9 | for j in raw: |
| 10 | if isinstance(job, (list, tuple)) and len(job) == 3: | 10 | if isinstance(j, (list, tuple)) and len(j) == 3: |
| 11 | s, e, w = job | 11 | jobs.append((j[1], j[0], j[2])) # sort by end, store as (end, start, weight) |
| 12 | jobs.append((s, e, w)) | 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(key=lambda x: (x[1], x[0], x[2])) | 17 | bests = [] |
| 18 | 18 | ||
| 19 | states_end = [float("-inf")] | 19 | for e, s, w in jobs: |
| 20 | states_val = [0] | 20 | lo = 0 |
| 21 | 21 | hi = len(ends) | |
| 22 | for s, e, w in jobs: | 22 | while lo < hi: |
| 23 | lo, hi = 0, len(states_end) | 23 | mid = (lo + hi) >> 1 |
| 24 | while lo < hi: | 24 | if ends[mid] <= s: |
| 25 | mid = (lo + hi) // 2 | 25 | lo = mid + 1 |
| 26 | if states_end[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 | base = states_val[lo - 1] | 30 | current = bests[-1] if bests else 0 |
| 31 | cand = base + w | 31 | |
| 32 | 32 | if candidate > current: | |
| 33 | if cand <= states_val[-1]: | 33 | if ends and ends[-1] == e: |
| 34 | continue | 34 | bests[-1] = candidate |
| 35 | 35 | else: | |
| 36 | if e == states_end[-1]: | 36 | ends.append(e) |
| 37 | states_val[-1] = cand | 37 | bests.append(candidate) |
| 38 | continue | 38 | |
| 39 | 39 | return bests[-1] if bests else 0 | |
| 40 | states_end.append(e) | 40 | |
| 41 | states_val.append(cand) | 41 | |
| 42 | 42 | ||
| 43 | return states_val[-1] | 43 |
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 job in raw:10 if isinstance(job, (list, tuple)) and len(job) == 3:11 s, e, w = job12 jobs.append((s, e, w))1314 if not jobs:15 return 01617 jobs.sort(key=lambda x: (x[1], x[0], x[2]))1819 states_end = [float("-inf")]20 states_val = [0]2122 for s, e, w in jobs:23 lo, hi = 0, len(states_end)24 while lo < hi:25 mid = (lo + hi) // 226 if states_end[mid] <= s:27 lo = mid + 128 else:29 hi = mid30 base = states_val[lo - 1]31 cand = base + w3233 if cand <= states_val[-1]:34 continue3536 if e == states_end[-1]:37 states_val[-1] = cand38 continue3940 states_end.append(e)41 states_val.append(cand)4243 return states_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