Solution #2db3327f-0f78-40f5-b207-8d844c855dfc

completed

Score

100% (5/5)

Runtime

29μs

Delta

+25.0% vs parent

Tied for best

Improved from parent

Solution Lineage

Current100%Improved from parent
3ae0505880%Same as parent
2baa50c180%Same as parent
ce0d4fdb80%Same as parent
ad9ca75f80%Same as parent
3277469d80%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

    valid = []
    for job in jobs:
        if isinstance(job, (list, tuple)) and len(job) == 3:
            s, e, w = job
            valid.append((s, e, w))

    if not valid:
        return 0

    valid.sort(key=lambda x: (x[0], x[1], x[2]))

    heap = []  # min-heap of (end, total_weight_if_taken)
    best_done = 0

    def heappush(h, item):
        h.append(item)
        i = len(h) - 1
        while i > 0:
            p = (i - 1) // 2
            if h[p][0] <= item[0]:
                break
            h[i] = h[p]
            i = p
        h[i] = item

    def heappop(h):
        root = h[0]
        last = h.pop()
        if h:
            i = 0
            n = len(h)
            while True:
                l = 2 * i + 1
                r = l + 1
                if l >= n:
                    break
                c = l
                if r < n and h[r][0] < h[l][0]:
                    c = r
                if h[c][0] >= last[0]:
                    break
                h[i] = h[c]
                i = c
            h[i] = last
        return root

    for s, e, w in valid:
        while heap and heap[0][0] <= s:
            ended, total = heappop(heap)
            if total > best_done:
                best_done = total
        heappush(heap, (e, best_done + w))

    ans = best_done
    while heap:
        ended, total = heappop(heap)
        if total > ans:
            ans = total

    return ans

Compare with Champion

Score Difference

Tied

Runtime Advantage

11μs slower

Code Size

65 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 valid = []6 return 0
7 for job in jobs:7
8 if isinstance(job, (list, tuple)) and len(job) == 3:8 jobs = []
9 s, e, w = job9 for j in raw:
10 valid.append((s, e, w))10 if isinstance(j, (list, tuple)) and len(j) == 3:
1111 jobs.append((j[1], j[0], j[2])) # sort by end, store as (end, start, weight)
12 if not valid:12 if not jobs:
13 return 013 return 0
1414
15 valid.sort(key=lambda x: (x[0], x[1], x[2]))15 jobs.sort()
1616 ends = []
17 heap = [] # min-heap of (end, total_weight_if_taken)17 bests = []
18 best_done = 018
1919 for e, s, w in jobs:
20 def heappush(h, item):20 lo = 0
21 h.append(item)21 hi = len(ends)
22 i = len(h) - 122 while lo < hi:
23 while i > 0:23 mid = (lo + hi) >> 1
24 p = (i - 1) // 224 if ends[mid] <= s:
25 if h[p][0] <= item[0]:25 lo = mid + 1
26 break26 else:
27 h[i] = h[p]27 hi = mid
28 i = p28
29 h[i] = item29 candidate = w + (bests[lo - 1] if lo else 0)
3030 current = bests[-1] if bests else 0
31 def heappop(h):31
32 root = h[0]32 if candidate > current:
33 last = h.pop()33 if ends and ends[-1] == e:
34 if h:34 bests[-1] = candidate
35 i = 035 else:
36 n = len(h)36 ends.append(e)
37 while True:37 bests.append(candidate)
38 l = 2 * i + 138
39 r = l + 139 return bests[-1] if bests else 0
40 if l >= n:40
41 break41
42 c = l42
43 if r < n and h[r][0] < h[l][0]:43
44 c = r44
45 if h[c][0] >= last[0]:45
46 break46
47 h[i] = h[c]47
48 i = c48
49 h[i] = last49
50 return root50
5151
52 for s, e, w in valid:52
53 while heap and heap[0][0] <= s:53
54 ended, total = heappop(heap)54
55 if total > best_done:55
56 best_done = total56
57 heappush(heap, (e, best_done + w))57
5858
59 ans = best_done59
60 while heap:60
61 ended, total = heappop(heap)61
62 if total > ans:62
63 ans = total63
6464
65 return ans65
Your Solution
100% (5/5)29μs
1def solve(input):
2 jobs = input.get("jobs", []) if isinstance(input, dict) else []
3 if not jobs:
4 return 0
5
6 valid = []
7 for job in jobs:
8 if isinstance(job, (list, tuple)) and len(job) == 3:
9 s, e, w = job
10 valid.append((s, e, w))
11
12 if not valid:
13 return 0
14
15 valid.sort(key=lambda x: (x[0], x[1], x[2]))
16
17 heap = [] # min-heap of (end, total_weight_if_taken)
18 best_done = 0
19
20 def heappush(h, item):
21 h.append(item)
22 i = len(h) - 1
23 while i > 0:
24 p = (i - 1) // 2
25 if h[p][0] <= item[0]:
26 break
27 h[i] = h[p]
28 i = p
29 h[i] = item
30
31 def heappop(h):
32 root = h[0]
33 last = h.pop()
34 if h:
35 i = 0
36 n = len(h)
37 while True:
38 l = 2 * i + 1
39 r = l + 1
40 if l >= n:
41 break
42 c = l
43 if r < n and h[r][0] < h[l][0]:
44 c = r
45 if h[c][0] >= last[0]:
46 break
47 h[i] = h[c]
48 i = c
49 h[i] = last
50 return root
51
52 for s, e, w in valid:
53 while heap and heap[0][0] <= s:
54 ended, total = heappop(heap)
55 if total > best_done:
56 best_done = total
57 heappush(heap, (e, best_done + w))
58
59 ans = best_done
60 while heap:
61 ended, total = heappop(heap)
62 if total > ans:
63 ans = total
64
65 return ans
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