Solution #66b5ba20-f743-40a8-b91a-2143dbb1c250
completedScore
100% (5/5)
Runtime
27μs
Delta
No change vs parent
Tied for best
Same as parent
Score
100% (5/5)
Runtime
27μ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 = []
for job in raw:
if isinstance(job, (list, tuple)) and len(job) == 3:
jobs.append((job[1], job[0], job[2])) # sort by end, keep (end, start, weight)
if not jobs:
return 0
jobs.sort()
n = len(jobs)
ends = [0] * n
for i in range(n):
ends[i] = jobs[i][0]
try:
from bisect import bisect_right
except Exception:
def bisect_right(a, x, lo=0, hi=None):
if hi is None:
hi = len(a)
while lo < hi:
mid = (lo + hi) >> 1
if x < a[mid]:
hi = mid
else:
lo = mid + 1
return lo
dp = [0] * (n + 1)
for i in range(1, n + 1):
e, s, w = jobs[i - 1]
j = bisect_right(ends, s, 0, i - 1)
take = w + dp[j]
skip = dp[i - 1]
dp[i] = take if take > skip else skip
return dp[n]Score Difference
Tied
Runtime Advantage
9μs slower
Code Size
44 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 | for job in raw: | 9 | for j in raw: |
| 10 | if isinstance(job, (list, tuple)) and len(job) == 3: | 10 | if isinstance(j, (list, tuple)) and len(j) == 3: |
| 11 | jobs.append((job[1], job[0], job[2])) # sort by end, keep (end, start, weight) | 11 | jobs.append((j[1], j[0], j[2])) # sort by end, store as (end, start, weight) |
| 12 | 12 | if not jobs: | |
| 13 | if not jobs: | 13 | return 0 |
| 14 | return 0 | 14 | |
| 15 | 15 | jobs.sort() | |
| 16 | jobs.sort() | 16 | ends = [] |
| 17 | n = len(jobs) | 17 | bests = [] |
| 18 | ends = [0] * n | 18 | |
| 19 | for i in range(n): | 19 | for e, s, w in jobs: |
| 20 | ends[i] = jobs[i][0] | 20 | lo = 0 |
| 21 | 21 | hi = len(ends) | |
| 22 | try: | 22 | while lo < hi: |
| 23 | from bisect import bisect_right | 23 | mid = (lo + hi) >> 1 |
| 24 | except Exception: | 24 | if ends[mid] <= s: |
| 25 | def bisect_right(a, x, lo=0, hi=None): | 25 | lo = mid + 1 |
| 26 | if hi is None: | 26 | else: |
| 27 | hi = len(a) | 27 | hi = mid |
| 28 | while lo < hi: | 28 | |
| 29 | mid = (lo + hi) >> 1 | 29 | candidate = w + (bests[lo - 1] if lo else 0) |
| 30 | if x < a[mid]: | 30 | current = bests[-1] if bests else 0 |
| 31 | hi = mid | 31 | |
| 32 | else: | 32 | if candidate > current: |
| 33 | lo = mid + 1 | 33 | if ends and ends[-1] == e: |
| 34 | return lo | 34 | bests[-1] = candidate |
| 35 | 35 | else: | |
| 36 | dp = [0] * (n + 1) | 36 | ends.append(e) |
| 37 | for i in range(1, n + 1): | 37 | bests.append(candidate) |
| 38 | e, s, w = jobs[i - 1] | 38 | |
| 39 | j = bisect_right(ends, s, 0, i - 1) | 39 | return bests[-1] if bests else 0 |
| 40 | take = w + dp[j] | 40 | |
| 41 | skip = dp[i - 1] | 41 | |
| 42 | dp[i] = take if take > skip else skip | 42 | |
| 43 | 43 | ||
| 44 | return dp[n] | 44 |
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 job in raw:10 if isinstance(job, (list, tuple)) and len(job) == 3:11 jobs.append((job[1], job[0], job[2])) # sort by end, keep (end, start, weight)1213 if not jobs:14 return 01516 jobs.sort()17 n = len(jobs)18 ends = [0] * n19 for i in range(n):20 ends[i] = jobs[i][0]2122 try:23 from bisect import bisect_right24 except Exception:25 def bisect_right(a, x, lo=0, hi=None):26 if hi is None:27 hi = len(a)28 while lo < hi:29 mid = (lo + hi) >> 130 if x < a[mid]:31 hi = mid32 else:33 lo = mid + 134 return lo3536 dp = [0] * (n + 1)37 for i in range(1, n + 1):38 e, s, w = jobs[i - 1]39 j = bisect_right(ends, s, 0, i - 1)40 take = w + dp[j]41 skip = dp[i - 1]42 dp[i] = take if take > skip else skip4344 return dp[n]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