Solution #d619fdf3-718a-499a-a9ea-ea80621d7fbe
completedScore
100% (5/5)
Runtime
45μs
Delta
+66.7% vs parent
Tied for best
Improved from parent
Score
100% (5/5)
Runtime
45μs
Delta
+66.7% 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 = []
times_set = set()
for j in raw:
if isinstance(j, (list, tuple)) and len(j) == 3:
s, e, w = j[0], j[1], j[2]
jobs.append((s, e, w))
times_set.add(s)
times_set.add(e)
if not jobs:
return 0
times = sorted(times_set)
m = len(times)
idx = {t: i for i, t in enumerate(times)}
ends_at = [[] for _ in range(m)]
for s, e, w in jobs:
ends_at[idx[e]].append((idx[s], w))
parent = list(range(m + 1))
best = [0] * m
def find(x):
while parent[x] != x:
parent[x] = parent[parent[x]]
x = parent[x]
return x
cur_best = 0
i = 0
while i < m:
if i > 0 and best[i] < cur_best:
best[i] = cur_best
bucket = ends_at[i]
j = 0
local_best = best[i]
while j < len(bucket):
s_idx, w = bucket[j]
cand = best[s_idx] + w
if cand > local_best:
local_best = cand
j += 1
if local_best < cur_best:
local_best = cur_best
best[i] = local_best
cur_best = local_best
parent[i] = find(i + 1)
i = find(i)
return cur_best if cur_best > 0 else 0Score Difference
Tied
Runtime Advantage
27μs slower
Code Size
63 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 | times_set = set() | 9 | for j in raw: |
| 10 | 10 | if isinstance(j, (list, tuple)) and len(j) == 3: | |
| 11 | for j in raw: | 11 | jobs.append((j[1], j[0], j[2])) # sort by end, store as (end, start, weight) |
| 12 | if isinstance(j, (list, tuple)) and len(j) == 3: | 12 | if not jobs: |
| 13 | s, e, w = j[0], j[1], j[2] | 13 | return 0 |
| 14 | jobs.append((s, e, w)) | 14 | |
| 15 | times_set.add(s) | 15 | jobs.sort() |
| 16 | times_set.add(e) | 16 | ends = [] |
| 17 | 17 | bests = [] | |
| 18 | if not jobs: | 18 | |
| 19 | return 0 | 19 | for e, s, w in jobs: |
| 20 | 20 | lo = 0 | |
| 21 | times = sorted(times_set) | 21 | hi = len(ends) |
| 22 | m = len(times) | 22 | while lo < hi: |
| 23 | idx = {t: i for i, t in enumerate(times)} | 23 | mid = (lo + hi) >> 1 |
| 24 | 24 | if ends[mid] <= s: | |
| 25 | ends_at = [[] for _ in range(m)] | 25 | lo = mid + 1 |
| 26 | for s, e, w in jobs: | 26 | else: |
| 27 | ends_at[idx[e]].append((idx[s], w)) | 27 | hi = mid |
| 28 | 28 | ||
| 29 | parent = list(range(m + 1)) | 29 | candidate = w + (bests[lo - 1] if lo else 0) |
| 30 | best = [0] * m | 30 | current = bests[-1] if bests else 0 |
| 31 | 31 | ||
| 32 | def find(x): | 32 | if candidate > current: |
| 33 | while parent[x] != x: | 33 | if ends and ends[-1] == e: |
| 34 | parent[x] = parent[parent[x]] | 34 | bests[-1] = candidate |
| 35 | x = parent[x] | 35 | else: |
| 36 | return x | 36 | ends.append(e) |
| 37 | 37 | bests.append(candidate) | |
| 38 | cur_best = 0 | 38 | |
| 39 | i = 0 | 39 | return bests[-1] if bests else 0 |
| 40 | while i < m: | 40 | |
| 41 | if i > 0 and best[i] < cur_best: | 41 | |
| 42 | best[i] = cur_best | 42 | |
| 43 | 43 | ||
| 44 | bucket = ends_at[i] | 44 | |
| 45 | j = 0 | 45 | |
| 46 | local_best = best[i] | 46 | |
| 47 | while j < len(bucket): | 47 | |
| 48 | s_idx, w = bucket[j] | 48 | |
| 49 | cand = best[s_idx] + w | 49 | |
| 50 | if cand > local_best: | 50 | |
| 51 | local_best = cand | 51 | |
| 52 | j += 1 | 52 | |
| 53 | 53 | ||
| 54 | if local_best < cur_best: | 54 | |
| 55 | local_best = cur_best | 55 | |
| 56 | 56 | ||
| 57 | best[i] = local_best | 57 | |
| 58 | cur_best = local_best | 58 | |
| 59 | 59 | ||
| 60 | parent[i] = find(i + 1) | 60 | |
| 61 | i = find(i) | 61 | |
| 62 | 62 | ||
| 63 | return cur_best if cur_best > 0 else 0 | 63 |
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 times_set = set()1011 for j in raw:12 if isinstance(j, (list, tuple)) and len(j) == 3:13 s, e, w = j[0], j[1], j[2]14 jobs.append((s, e, w))15 times_set.add(s)16 times_set.add(e)1718 if not jobs:19 return 02021 times = sorted(times_set)22 m = len(times)23 idx = {t: i for i, t in enumerate(times)}2425 ends_at = [[] for _ in range(m)]26 for s, e, w in jobs:27 ends_at[idx[e]].append((idx[s], w))2829 parent = list(range(m + 1))30 best = [0] * m3132 def find(x):33 while parent[x] != x:34 parent[x] = parent[parent[x]]35 x = parent[x]36 return x3738 cur_best = 039 i = 040 while i < m:41 if i > 0 and best[i] < cur_best:42 best[i] = cur_best4344 bucket = ends_at[i]45 j = 046 local_best = best[i]47 while j < len(bucket):48 s_idx, w = bucket[j]49 cand = best[s_idx] + w50 if cand > local_best:51 local_best = cand52 j += 15354 if local_best < cur_best:55 local_best = cur_best5657 best[i] = local_best58 cur_best = local_best5960 parent[i] = find(i + 1)61 i = find(i)6263 return cur_best if cur_best > 0 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