Solution #2524a75f-e824-43e9-a5a3-5f546f8697c6
completedScore
80% (4/5)
Runtime
32μs
Delta
No change vs parent
-20.0% vs best
Same as parent
Score
80% (4/5)
Runtime
32μs
Delta
No change vs parent
-20.0% vs best
Same as parent
def solve(input):
jobs = input.get("jobs", [])
if not jobs:
return 0
# Coordinate compression + time-based DP.
# dp[t] = best achievable weight up to compressed time t.
times = []
norm_jobs = []
for job in jobs:
s, e, w = job
norm_jobs.append((s, e, w))
times.append(s)
times.append(e)
times = sorted(set(times))
idx = {}
for i, t in enumerate(times):
idx[t] = i
m = len(times)
ending = [[] for _ in range(m)]
for s, e, w in norm_jobs:
ending[idx[e]].append((idx[s], w))
dp = [0] * m
best = 0
for i in range(m):
if i > 0 and dp[i - 1] > best:
best = dp[i - 1]
cur = best
for s_idx, w in ending[i]:
cand = dp[s_idx] + w
if cand > cur:
cur = cand
dp[i] = cur
return dp[-1]Score Difference
-20.0%
Runtime Advantage
14μs slower
Code Size
38 vs 39 lines
| # | Your Solution | # | Champion |
|---|---|---|---|
| 1 | def solve(input): | 1 | def solve(input): |
| 2 | jobs = input.get("jobs", []) | 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 | # Coordinate compression + time-based DP. | 6 | return 0 |
| 7 | # dp[t] = best achievable weight up to compressed time t. | 7 | |
| 8 | times = [] | 8 | jobs = [] |
| 9 | norm_jobs = [] | 9 | for j in raw: |
| 10 | for job in jobs: | 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_jobs.append((s, e, w)) | 12 | if not jobs: |
| 13 | times.append(s) | 13 | return 0 |
| 14 | times.append(e) | 14 | |
| 15 | 15 | jobs.sort() | |
| 16 | times = sorted(set(times)) | 16 | ends = [] |
| 17 | idx = {} | 17 | bests = [] |
| 18 | for i, t in enumerate(times): | 18 | |
| 19 | idx[t] = i | 19 | for e, s, w in jobs: |
| 20 | 20 | lo = 0 | |
| 21 | m = len(times) | 21 | hi = len(ends) |
| 22 | ending = [[] for _ in range(m)] | 22 | while lo < hi: |
| 23 | for s, e, w in norm_jobs: | 23 | mid = (lo + hi) >> 1 |
| 24 | ending[idx[e]].append((idx[s], w)) | 24 | if ends[mid] <= s: |
| 25 | 25 | lo = mid + 1 | |
| 26 | dp = [0] * m | 26 | else: |
| 27 | best = 0 | 27 | hi = mid |
| 28 | for i in range(m): | 28 | |
| 29 | if i > 0 and dp[i - 1] > best: | 29 | candidate = w + (bests[lo - 1] if lo else 0) |
| 30 | best = dp[i - 1] | 30 | current = bests[-1] if bests else 0 |
| 31 | cur = best | 31 | |
| 32 | for s_idx, w in ending[i]: | 32 | if candidate > current: |
| 33 | cand = dp[s_idx] + w | 33 | if ends and ends[-1] == e: |
| 34 | if cand > cur: | 34 | bests[-1] = candidate |
| 35 | cur = cand | 35 | else: |
| 36 | dp[i] = cur | 36 | ends.append(e) |
| 37 | 37 | bests.append(candidate) | |
| 38 | return dp[-1] | 38 | |
| 39 | 39 | return bests[-1] if bests else 0 |
1def solve(input):2 jobs = input.get("jobs", [])3 if not jobs:4 return 056 # Coordinate compression + time-based DP.7 # dp[t] = best achievable weight up to compressed time t.8 times = []9 norm_jobs = []10 for job in jobs:11 s, e, w = job12 norm_jobs.append((s, e, w))13 times.append(s)14 times.append(e)1516 times = sorted(set(times))17 idx = {}18 for i, t in enumerate(times):19 idx[t] = i2021 m = len(times)22 ending = [[] for _ in range(m)]23 for s, e, w in norm_jobs:24 ending[idx[e]].append((idx[s], w))2526 dp = [0] * m27 best = 028 for i in range(m):29 if i > 0 and dp[i - 1] > best:30 best = dp[i - 1]31 cur = best32 for s_idx, w in ending[i]:33 cand = dp[s_idx] + w34 if cand > cur:35 cur = cand36 dp[i] = cur3738 return dp[-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