Solution #ce0d4fdb-e3b8-4689-9cee-eb1228e2983b
completedScore
80% (4/5)
Runtime
35μs
Delta
No change vs parent
-20.0% vs best
Same as parent
Score
80% (4/5)
Runtime
35μs
Delta
No change vs parent
-20.0% vs best
Same as parent
def solve(input):
jobs = input.get("jobs", []) if isinstance(input, dict) else []
if not jobs:
return 0
norm = []
times = set()
for job in jobs:
if not isinstance(job, (list, tuple)) or len(job) != 3:
continue
s, e, w = job
norm.append((s, e, w))
times.add(s)
times.add(e)
if not norm:
return 0
coords = sorted(times)
m = len(coords)
idx = {t: i for i, t in enumerate(coords)}
buckets = [[] for _ in range(m)]
for s, e, w in norm:
buckets[idx[e]].append((idx[s], w))
parent = list(range(m + 1))
def find(x):
while parent[x] != x:
parent[x] = parent[parent[x]]
x = parent[x]
return x
dp = [0] * m
current_best = 0
for i in range(m):
dp[i] = current_best
for s_idx, w in buckets[i]:
cand = dp[s_idx] + w
if cand > dp[i]:
dp[i] = cand
if dp[i] > current_best:
current_best = dp[i]
parent[i] = find(i + 1)
return current_bestScore Difference
-20.0%
Runtime Advantage
17μs slower
Code Size
51 vs 39 lines
| # | Your Solution | # | Champion |
|---|---|---|---|
| 1 | def solve(input): | 1 | def solve(input): |
| 2 | jobs = input.get("jobs", []) if isinstance(input, dict) else [] | 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 | norm = [] | 6 | return 0 |
| 7 | times = set() | 7 | |
| 8 | for job in jobs: | 8 | jobs = [] |
| 9 | if not isinstance(job, (list, tuple)) or len(job) != 3: | 9 | for j in raw: |
| 10 | continue | 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 | norm.append((s, e, w)) | 12 | if not jobs: |
| 13 | times.add(s) | 13 | return 0 |
| 14 | times.add(e) | 14 | |
| 15 | 15 | jobs.sort() | |
| 16 | if not norm: | 16 | ends = [] |
| 17 | return 0 | 17 | bests = [] |
| 18 | 18 | ||
| 19 | coords = sorted(times) | 19 | for e, s, w in jobs: |
| 20 | m = len(coords) | 20 | lo = 0 |
| 21 | idx = {t: i for i, t in enumerate(coords)} | 21 | hi = len(ends) |
| 22 | 22 | while lo < hi: | |
| 23 | buckets = [[] for _ in range(m)] | 23 | mid = (lo + hi) >> 1 |
| 24 | for s, e, w in norm: | 24 | if ends[mid] <= s: |
| 25 | buckets[idx[e]].append((idx[s], w)) | 25 | lo = mid + 1 |
| 26 | 26 | else: | |
| 27 | parent = list(range(m + 1)) | 27 | hi = mid |
| 28 | 28 | ||
| 29 | def find(x): | 29 | candidate = w + (bests[lo - 1] if lo else 0) |
| 30 | while parent[x] != x: | 30 | current = bests[-1] if bests else 0 |
| 31 | parent[x] = parent[parent[x]] | 31 | |
| 32 | x = parent[x] | 32 | if candidate > current: |
| 33 | return x | 33 | if ends and ends[-1] == e: |
| 34 | 34 | bests[-1] = candidate | |
| 35 | dp = [0] * m | 35 | else: |
| 36 | current_best = 0 | 36 | ends.append(e) |
| 37 | 37 | bests.append(candidate) | |
| 38 | for i in range(m): | 38 | |
| 39 | dp[i] = current_best | 39 | return bests[-1] if bests else 0 |
| 40 | 40 | ||
| 41 | for s_idx, w in buckets[i]: | 41 | |
| 42 | cand = dp[s_idx] + w | 42 | |
| 43 | if cand > dp[i]: | 43 | |
| 44 | dp[i] = cand | 44 | |
| 45 | 45 | ||
| 46 | if dp[i] > current_best: | 46 | |
| 47 | current_best = dp[i] | 47 | |
| 48 | 48 | ||
| 49 | parent[i] = find(i + 1) | 49 | |
| 50 | 50 | ||
| 51 | return current_best | 51 |
1def solve(input):2 jobs = input.get("jobs", []) if isinstance(input, dict) else []3 if not jobs:4 return 056 norm = []7 times = set()8 for job in jobs:9 if not isinstance(job, (list, tuple)) or len(job) != 3:10 continue11 s, e, w = job12 norm.append((s, e, w))13 times.add(s)14 times.add(e)1516 if not norm:17 return 01819 coords = sorted(times)20 m = len(coords)21 idx = {t: i for i, t in enumerate(coords)}2223 buckets = [[] for _ in range(m)]24 for s, e, w in norm:25 buckets[idx[e]].append((idx[s], w))2627 parent = list(range(m + 1))2829 def find(x):30 while parent[x] != x:31 parent[x] = parent[parent[x]]32 x = parent[x]33 return x3435 dp = [0] * m36 current_best = 03738 for i in range(m):39 dp[i] = current_best4041 for s_idx, w in buckets[i]:42 cand = dp[s_idx] + w43 if cand > dp[i]:44 dp[i] = cand4546 if dp[i] > current_best:47 current_best = dp[i]4849 parent[i] = find(i + 1)5051 return current_best1def 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