Solution #41f00984-0a11-4e61-8f6d-d1a04b53429c
completedScore
20% (1/5)
Runtime
43μs
Delta
-80.0% vs parent
-80.0% vs best
Regression from parent
Score
20% (1/5)
Runtime
43μs
Delta
-80.0% vs parent
-80.0% vs best
Regression from parent
def solve(input):
if not isinstance(input, dict):
return 0
raw = input.get("jobs")
if not raw:
return 0
jobs = []
starts = []
ends = []
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))
starts.append(s)
ends.append(e)
if not jobs:
return 0
times = sorted(set(starts + ends))
m = len(times)
index = {}
for i, t in enumerate(times):
index[t] = i
best_start = [0] * m
for i, t in enumerate(times):
if t in index:
best_start[i] = index[t]
prefix_lookup = [0] * m
if m:
prefix_lookup[0] = 0
for i in range(1, m):
prefix_lookup[i] = i - 1
by_end = [[] for _ in range(m)]
for s, e, w in jobs:
by_end[index[e]].append((index[s], w))
dp = [0] * m
for ei in range(m):
best = dp[ei - 1] if ei else 0
bucket = by_end[ei]
for si, w in bucket:
prev = dp[prefix_lookup[si]] if si > 0 else 0
cand = prev + w
if cand > best:
best = cand
dp[ei] = best
return dp[-1] if dp else 0Score Difference
-80.0%
Runtime Advantage
25μs slower
Code Size
54 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 | starts = [] | 9 | for j in raw: |
| 10 | ends = [] | 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 | starts.append(s) | 15 | jobs.sort() |
| 16 | ends.append(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(set(starts + ends)) | 21 | hi = len(ends) |
| 22 | m = len(times) | 22 | while lo < hi: |
| 23 | 23 | mid = (lo + hi) >> 1 | |
| 24 | index = {} | 24 | if ends[mid] <= s: |
| 25 | for i, t in enumerate(times): | 25 | lo = mid + 1 |
| 26 | index[t] = i | 26 | else: |
| 27 | 27 | hi = mid | |
| 28 | best_start = [0] * m | 28 | |
| 29 | for i, t in enumerate(times): | 29 | candidate = w + (bests[lo - 1] if lo else 0) |
| 30 | if t in index: | 30 | current = bests[-1] if bests else 0 |
| 31 | best_start[i] = index[t] | 31 | |
| 32 | 32 | if candidate > current: | |
| 33 | prefix_lookup = [0] * m | 33 | if ends and ends[-1] == e: |
| 34 | if m: | 34 | bests[-1] = candidate |
| 35 | prefix_lookup[0] = 0 | 35 | else: |
| 36 | for i in range(1, m): | 36 | ends.append(e) |
| 37 | prefix_lookup[i] = i - 1 | 37 | bests.append(candidate) |
| 38 | 38 | ||
| 39 | by_end = [[] for _ in range(m)] | 39 | return bests[-1] if bests else 0 |
| 40 | for s, e, w in jobs: | 40 | |
| 41 | by_end[index[e]].append((index[s], w)) | 41 | |
| 42 | 42 | ||
| 43 | dp = [0] * m | 43 | |
| 44 | for ei in range(m): | 44 | |
| 45 | best = dp[ei - 1] if ei else 0 | 45 | |
| 46 | bucket = by_end[ei] | 46 | |
| 47 | for si, w in bucket: | 47 | |
| 48 | prev = dp[prefix_lookup[si]] if si > 0 else 0 | 48 | |
| 49 | cand = prev + w | 49 | |
| 50 | if cand > best: | 50 | |
| 51 | best = cand | 51 | |
| 52 | dp[ei] = best | 52 | |
| 53 | 53 | ||
| 54 | return dp[-1] if dp else 0 | 54 |
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 starts = []10 ends = []11 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 starts.append(s)16 ends.append(e)1718 if not jobs:19 return 02021 times = sorted(set(starts + ends))22 m = len(times)2324 index = {}25 for i, t in enumerate(times):26 index[t] = i2728 best_start = [0] * m29 for i, t in enumerate(times):30 if t in index:31 best_start[i] = index[t]3233 prefix_lookup = [0] * m34 if m:35 prefix_lookup[0] = 036 for i in range(1, m):37 prefix_lookup[i] = i - 13839 by_end = [[] for _ in range(m)]40 for s, e, w in jobs:41 by_end[index[e]].append((index[s], w))4243 dp = [0] * m44 for ei in range(m):45 best = dp[ei - 1] if ei else 046 bucket = by_end[ei]47 for si, w in bucket:48 prev = dp[prefix_lookup[si]] if si > 0 else 049 cand = prev + w50 if cand > best:51 best = cand52 dp[ei] = best5354 return dp[-1] if dp 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