Solution #f23f1e97-6af3-48c1-88be-c5efc1832324
completedScore
100% (5/5)
Runtime
34μs
Delta
No change vs parent
Tied for best
Same as parent
Score
100% (5/5)
Runtime
34μs
Delta
No change vs parent
Tied for best
Same as parent
def solve(input):
if not isinstance(input, dict):
return 0
raw = input.get("jobs")
if not raw:
return 0
jobs = []
times = []
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.append(s)
times.append(e)
if not jobs:
return 0
times = sorted(set(times))
m = len(times)
time_to_idx = {}
for i in range(m):
time_to_idx[times[i]] = i
end_buckets = [[] for _ in range(m)]
for s, e, w in jobs:
end_buckets[time_to_idx[e]].append((time_to_idx[s], w))
dp = [0] * m
best = 0
for i in range(m):
cur = best
bucket = end_buckets[i]
for s_idx, w in bucket:
cand = dp[s_idx] + w
if cand > cur:
cur = cand
dp[i] = cur
best = cur
return bestScore Difference
Tied
Runtime Advantage
16μs slower
Code Size
42 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 = [] | 9 | for j in raw: |
| 10 | for j in raw: | 10 | if isinstance(j, (list, tuple)) and len(j) == 3: |
| 11 | 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 | s, e, w = j[0], j[1], j[2] | 12 | if not jobs: |
| 13 | jobs.append((s, e, w)) | 13 | return 0 |
| 14 | times.append(s) | 14 | |
| 15 | times.append(e) | 15 | jobs.sort() |
| 16 | if not jobs: | 16 | ends = [] |
| 17 | return 0 | 17 | bests = [] |
| 18 | 18 | ||
| 19 | times = sorted(set(times)) | 19 | for e, s, w in jobs: |
| 20 | m = len(times) | 20 | lo = 0 |
| 21 | 21 | hi = len(ends) | |
| 22 | time_to_idx = {} | 22 | while lo < hi: |
| 23 | for i in range(m): | 23 | mid = (lo + hi) >> 1 |
| 24 | time_to_idx[times[i]] = i | 24 | if ends[mid] <= s: |
| 25 | 25 | lo = mid + 1 | |
| 26 | end_buckets = [[] for _ in range(m)] | 26 | else: |
| 27 | for s, e, w in jobs: | 27 | hi = mid |
| 28 | end_buckets[time_to_idx[e]].append((time_to_idx[s], w)) | 28 | |
| 29 | 29 | candidate = w + (bests[lo - 1] if lo else 0) | |
| 30 | dp = [0] * m | 30 | current = bests[-1] if bests else 0 |
| 31 | best = 0 | 31 | |
| 32 | for i in range(m): | 32 | if candidate > current: |
| 33 | cur = best | 33 | if ends and ends[-1] == e: |
| 34 | bucket = end_buckets[i] | 34 | bests[-1] = candidate |
| 35 | for s_idx, w in bucket: | 35 | else: |
| 36 | cand = dp[s_idx] + w | 36 | ends.append(e) |
| 37 | if cand > cur: | 37 | bests.append(candidate) |
| 38 | cur = cand | 38 | |
| 39 | dp[i] = cur | 39 | return bests[-1] if bests else 0 |
| 40 | best = cur | 40 | |
| 41 | 41 | ||
| 42 | return best | 42 |
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 = []10 for j in raw:11 if isinstance(j, (list, tuple)) and len(j) == 3:12 s, e, w = j[0], j[1], j[2]13 jobs.append((s, e, w))14 times.append(s)15 times.append(e)16 if not jobs:17 return 01819 times = sorted(set(times))20 m = len(times)2122 time_to_idx = {}23 for i in range(m):24 time_to_idx[times[i]] = i2526 end_buckets = [[] for _ in range(m)]27 for s, e, w in jobs:28 end_buckets[time_to_idx[e]].append((time_to_idx[s], w))2930 dp = [0] * m31 best = 032 for i in range(m):33 cur = best34 bucket = end_buckets[i]35 for s_idx, w in bucket:36 cand = dp[s_idx] + w37 if cand > cur:38 cur = cand39 dp[i] = cur40 best = cur4142 return 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