Solution #14fae373-b5ae-42c4-a7be-27c81b954e29

completed

Score

100% (5/5)

Runtime

25μs

Delta

+400.0% vs parent

Tied for best

Improved from parent

Solution Lineage

Current100%Improved from parent
dbb1f88620%Regression from parent
1c204190100%Same as parent
37d6e851100%Same as parent
58b73178100%Same as parent
66b5ba20100%Same as parent
6773a310100%Improved from parent
530effa860%Regression from parent
5bd2ddd7100%Same as parent
eef58d00100%Improved from parent
0c1780d720%Regression from parent
2db3327f100%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):
    if not isinstance(input, dict):
        return 0
    raw = input.get("jobs")
    if not raw:
        return 0

    jobs = []
    for j in raw:
        if isinstance(j, (list, tuple)) and len(j) == 3:
            jobs.append((j[0], j[1], j[2]))
    if not jobs:
        return 0

    jobs.sort(key=lambda x: x[1])

    frontier = [(float("-inf"), 0)]  # (end_time, best_weight_up_to_end)

    for s, e, w in jobs:
        lo = 0
        hi = len(frontier)
        while lo < hi:
            mid = (lo + hi) >> 1
            if frontier[mid][0] <= s:
                lo = mid + 1
            else:
                hi = mid
        base = frontier[lo - 1][1]
        cand = base + w

        if cand <= frontier[-1][1]:
            continue

        if e == frontier[-1][0]:
            frontier[-1] = (e, cand)
        else:
            frontier.append((e, cand))

    return frontier[-1][1]

Compare with Champion

Score Difference

Tied

Runtime Advantage

7μs slower

Code Size

39 vs 39 lines

#Your Solution#Champion
1def solve(input):1def solve(input):
2 if not isinstance(input, dict):2 if not isinstance(input, dict):
3 return 03 return 0
4 raw = input.get("jobs")4 raw = input.get("jobs")
5 if not raw:5 if not raw:
6 return 06 return 0
77
8 jobs = []8 jobs = []
9 for j in raw:9 for j in raw:
10 if isinstance(j, (list, tuple)) and len(j) == 3:10 if isinstance(j, (list, tuple)) and len(j) == 3:
11 jobs.append((j[0], j[1], j[2]))11 jobs.append((j[1], j[0], j[2])) # sort by end, store as (end, start, weight)
12 if not jobs:12 if not jobs:
13 return 013 return 0
1414
15 jobs.sort(key=lambda x: x[1])15 jobs.sort()
1616 ends = []
17 frontier = [(float("-inf"), 0)] # (end_time, best_weight_up_to_end)17 bests = []
1818
19 for s, e, w in jobs:19 for e, s, w in jobs:
20 lo = 020 lo = 0
21 hi = len(frontier)21 hi = len(ends)
22 while lo < hi:22 while lo < hi:
23 mid = (lo + hi) >> 123 mid = (lo + hi) >> 1
24 if frontier[mid][0] <= s:24 if ends[mid] <= s:
25 lo = mid + 125 lo = mid + 1
26 else:26 else:
27 hi = mid27 hi = mid
28 base = frontier[lo - 1][1]28
29 cand = base + w29 candidate = w + (bests[lo - 1] if lo else 0)
3030 current = bests[-1] if bests else 0
31 if cand <= frontier[-1][1]:31
32 continue32 if candidate > current:
3333 if ends and ends[-1] == e:
34 if e == frontier[-1][0]:34 bests[-1] = candidate
35 frontier[-1] = (e, cand)35 else:
36 else:36 ends.append(e)
37 frontier.append((e, cand))37 bests.append(candidate)
3838
39 return frontier[-1][1]39 return bests[-1] if bests else 0
Your Solution
100% (5/5)25μ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[0], j[1], j[2]))
12 if not jobs:
13 return 0
14
15 jobs.sort(key=lambda x: x[1])
16
17 frontier = [(float("-inf"), 0)] # (end_time, best_weight_up_to_end)
18
19 for s, e, w in jobs:
20 lo = 0
21 hi = len(frontier)
22 while lo < hi:
23 mid = (lo + hi) >> 1
24 if frontier[mid][0] <= s:
25 lo = mid + 1
26 else:
27 hi = mid
28 base = frontier[lo - 1][1]
29 cand = base + w
30
31 if cand <= frontier[-1][1]:
32 continue
33
34 if e == frontier[-1][0]:
35 frontier[-1] = (e, cand)
36 else:
37 frontier.append((e, cand))
38
39 return frontier[-1][1]
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