Solution #3277469d-afb6-4f98-99fc-b48cc79be71b

completed

Score

80% (4/5)

Runtime

31μs

Delta

+100.0% vs parent

-20.0% vs best

Improved from parent

Solution Lineage

Current80%Improved from parent
f050941640%Regression from parent
8ec5143380%Same as parent
da62d7c880%Same as parent
3d4c689e80%Same as parent
1f2128e780%Improved from parent
92397d700%Regression from parent
7cbd604b80%Improved from parent
fc42ee8d0%Regression from parent
b0db196680%Same as parent
692ab45880%Same as parent
8996709780%Improved from parent
17dd25090%Regression from parent
2524a75f80%Same as parent
e8534c5f80%Same as parent
bc1ec94080%Same as parent
7630bd9e80%Same as parent
ae983b2480%Same as parent
8252644380%Same as parent
9db0fb8580%Improved from parent
df1d6fd340%Regression from parent
ae833bc280%Same as parent
c90b917a80%First in chain

Code

def solve(input):
    jobs = input.get("jobs", []) if isinstance(input, dict) else []
    if not jobs:
        return 0

    times = {}
    idx = 0
    normalized = []
    for job in jobs:
        s, e, w = job
        normalized.append((s, e, w))
        if s not in times:
            times[s] = 0
        if e not in times:
            times[e] = 0

    sorted_times = sorted(times)
    for i, t in enumerate(sorted_times):
        times[t] = i

    m = len(sorted_times)
    end_buckets = [[] for _ in range(m)]
    for s, e, w in normalized:
        end_buckets[times[e]].append((times[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 end_buckets[i]:
            candidate = (dp[s_idx] if s_idx >= 0 else 0) + w
            if candidate > cur:
                cur = candidate
        dp[i] = cur

    return dp[-1] if dp else 0

Compare with Champion

Score Difference

-20.0%

Runtime Advantage

13μs slower

Code Size

38 vs 39 lines

#Your Solution#Champion
1def solve(input):1def 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 04 raw = input.get("jobs")
55 if not raw:
6 times = {}6 return 0
7 idx = 07
8 normalized = []8 jobs = []
9 for job in jobs:9 for j in raw:
10 s, e, w = job10 if isinstance(j, (list, tuple)) and len(j) == 3:
11 normalized.append((s, e, w))11 jobs.append((j[1], j[0], j[2])) # sort by end, store as (end, start, weight)
12 if s not in times:12 if not jobs:
13 times[s] = 013 return 0
14 if e not in times:14
15 times[e] = 015 jobs.sort()
1616 ends = []
17 sorted_times = sorted(times)17 bests = []
18 for i, t in enumerate(sorted_times):18
19 times[t] = i19 for e, s, w in jobs:
2020 lo = 0
21 m = len(sorted_times)21 hi = len(ends)
22 end_buckets = [[] for _ in range(m)]22 while lo < hi:
23 for s, e, w in normalized:23 mid = (lo + hi) >> 1
24 end_buckets[times[e]].append((times[s], w))24 if ends[mid] <= s:
2525 lo = mid + 1
26 dp = [0] * m26 else:
27 best = 027 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 = best31
32 for s_idx, w in end_buckets[i]:32 if candidate > current:
33 candidate = (dp[s_idx] if s_idx >= 0 else 0) + w33 if ends and ends[-1] == e:
34 if candidate > cur:34 bests[-1] = candidate
35 cur = candidate35 else:
36 dp[i] = cur36 ends.append(e)
3737 bests.append(candidate)
38 return dp[-1] if dp else 038
3939 return bests[-1] if bests else 0
Your Solution
80% (4/5)31μs
1def solve(input):
2 jobs = input.get("jobs", []) if isinstance(input, dict) else []
3 if not jobs:
4 return 0
5
6 times = {}
7 idx = 0
8 normalized = []
9 for job in jobs:
10 s, e, w = job
11 normalized.append((s, e, w))
12 if s not in times:
13 times[s] = 0
14 if e not in times:
15 times[e] = 0
16
17 sorted_times = sorted(times)
18 for i, t in enumerate(sorted_times):
19 times[t] = i
20
21 m = len(sorted_times)
22 end_buckets = [[] for _ in range(m)]
23 for s, e, w in normalized:
24 end_buckets[times[e]].append((times[s], w))
25
26 dp = [0] * m
27 best = 0
28 for i in range(m):
29 if i > 0 and dp[i - 1] > best:
30 best = dp[i - 1]
31 cur = best
32 for s_idx, w in end_buckets[i]:
33 candidate = (dp[s_idx] if s_idx >= 0 else 0) + w
34 if candidate > cur:
35 cur = candidate
36 dp[i] = cur
37
38 return dp[-1] if dp else 0
Champion
100% (5/5)18μs
1def solve(input):
2 if not isinstance(input, dict):
3 return 0
4 raw = input.get("jobs")
5 if not raw:
6 return 0
7
8 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 0
14
15 jobs.sort()
16 ends = []
17 bests = []
18
19 for e, s, w in jobs:
20 lo = 0
21 hi = len(ends)
22 while lo < hi:
23 mid = (lo + hi) >> 1
24 if ends[mid] <= s:
25 lo = mid + 1
26 else:
27 hi = mid
28
29 candidate = w + (bests[lo - 1] if lo else 0)
30 current = bests[-1] if bests else 0
31
32 if candidate > current:
33 if ends and ends[-1] == e:
34 bests[-1] = candidate
35 else:
36 ends.append(e)
37 bests.append(candidate)
38
39 return bests[-1] if bests else 0