Solution #e8534c5f-5c09-46e5-8c1d-af6f5ce4ca38
completedScore
80% (4/5)
Runtime
42μs
Delta
No change vs parent
-20.0% vs best
Same as parent
Score
80% (4/5)
Runtime
42μs
Delta
No change vs parent
-20.0% vs best
Same as parent
def solve(input):
jobs = input.get("jobs", [])
if not jobs:
return 0
jobs = sorted(map(tuple, jobs), key=lambda x: x[1])
ends = [e for _, e, _ in jobs]
n = len(jobs)
def upper_bound(arr, target, lo=0, hi=None):
hi = len(arr) if hi is None else hi
return lo if lo >= hi else (
upper_bound(arr, target, (lo + hi) // 2 + 1, hi)
if arr[(lo + hi) // 2] <= target
else upper_bound(arr, target, lo, (lo + hi) // 2)
)
p = [upper_bound(ends, jobs[i][0], 0, i) - 1 for i in range(n)]
memo = {}
def dp(i):
if i < 0:
return 0
if i in memo:
return memo[i]
memo[i] = max(dp(i - 1), jobs[i][2] + dp(p[i]))
return memo[i]
return dp(n - 1)Score Difference
-20.0%
Runtime Advantage
24μs slower
Code Size
29 vs 39 lines
| # | Your Solution | # | Champion |
|---|---|---|---|
| 1 | def solve(input): | 1 | def solve(input): |
| 2 | jobs = input.get("jobs", []) | 2 | if not isinstance(input, dict): |
| 3 | if not jobs: | 3 | return 0 |
| 4 | return 0 | 4 | raw = input.get("jobs") |
| 5 | 5 | if not raw: | |
| 6 | jobs = sorted(map(tuple, jobs), key=lambda x: x[1]) | 6 | return 0 |
| 7 | ends = [e for _, e, _ in jobs] | 7 | |
| 8 | n = len(jobs) | 8 | jobs = [] |
| 9 | 9 | for j in raw: | |
| 10 | def upper_bound(arr, target, lo=0, hi=None): | 10 | if isinstance(j, (list, tuple)) and len(j) == 3: |
| 11 | hi = len(arr) if hi is None else hi | 11 | jobs.append((j[1], j[0], j[2])) # sort by end, store as (end, start, weight) |
| 12 | return lo if lo >= hi else ( | 12 | if not jobs: |
| 13 | upper_bound(arr, target, (lo + hi) // 2 + 1, hi) | 13 | return 0 |
| 14 | if arr[(lo + hi) // 2] <= target | 14 | |
| 15 | else upper_bound(arr, target, lo, (lo + hi) // 2) | 15 | jobs.sort() |
| 16 | ) | 16 | ends = [] |
| 17 | 17 | bests = [] | |
| 18 | p = [upper_bound(ends, jobs[i][0], 0, i) - 1 for i in range(n)] | 18 | |
| 19 | memo = {} | 19 | for e, s, w in jobs: |
| 20 | 20 | lo = 0 | |
| 21 | def dp(i): | 21 | hi = len(ends) |
| 22 | if i < 0: | 22 | while lo < hi: |
| 23 | return 0 | 23 | mid = (lo + hi) >> 1 |
| 24 | if i in memo: | 24 | if ends[mid] <= s: |
| 25 | return memo[i] | 25 | lo = mid + 1 |
| 26 | memo[i] = max(dp(i - 1), jobs[i][2] + dp(p[i])) | 26 | else: |
| 27 | return memo[i] | 27 | hi = mid |
| 28 | 28 | ||
| 29 | return dp(n - 1) | 29 | candidate = w + (bests[lo - 1] if lo else 0) |
| 30 | 30 | current = bests[-1] if bests else 0 | |
| 31 | 31 | ||
| 32 | 32 | if candidate > current: | |
| 33 | 33 | if ends and ends[-1] == e: | |
| 34 | 34 | bests[-1] = candidate | |
| 35 | 35 | else: | |
| 36 | 36 | ends.append(e) | |
| 37 | 37 | bests.append(candidate) | |
| 38 | 38 | ||
| 39 | 39 | return bests[-1] if bests else 0 |
1def solve(input):2 jobs = input.get("jobs", [])3 if not jobs:4 return 056 jobs = sorted(map(tuple, jobs), key=lambda x: x[1])7 ends = [e for _, e, _ in jobs]8 n = len(jobs)910 def upper_bound(arr, target, lo=0, hi=None):11 hi = len(arr) if hi is None else hi12 return lo if lo >= hi else (13 upper_bound(arr, target, (lo + hi) // 2 + 1, hi)14 if arr[(lo + hi) // 2] <= target15 else upper_bound(arr, target, lo, (lo + hi) // 2)16 )1718 p = [upper_bound(ends, jobs[i][0], 0, i) - 1 for i in range(n)]19 memo = {}2021 def dp(i):22 if i < 0:23 return 024 if i in memo:25 return memo[i]26 memo[i] = max(dp(i - 1), jobs[i][2] + dp(p[i]))27 return memo[i]2829 return dp(n - 1)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