Solution #9462a7c0-e191-4f7c-94c8-f3300563e6a1
completedScore
100% (5/5)
Runtime
48μs
Delta
No change vs parent
Tied for best
Same as parent
Score
100% (5/5)
Runtime
48μ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 = tuple(filter(lambda j: isinstance(j, (list, tuple)) and len(j) == 3, raw))
if not jobs:
return 0
jobs = sorted(map(tuple, jobs), key=lambda x: (x[1], x[0], x[2]))
ends = tuple(map(lambda x: x[1], jobs))
def br(a, x, hi):
lo = 0
while lo < hi:
mid = (lo + hi) >> 1
if a[mid] <= x:
lo = mid + 1
else:
hi = mid
return lo
def step(state, job):
dp = state
s, e, w = job
i = br(ends, s, len(dp))
take = w + dp[i]
best = take if take > dp[-1] else dp[-1]
return dp + (best,)
return __import__("functools").reduce(step, jobs, (0,))[-1]Score Difference
Tied
Runtime Advantage
30μs slower
Code Size
33 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 = tuple(filter(lambda j: isinstance(j, (list, tuple)) and len(j) == 3, raw)) | 8 | jobs = [] |
| 9 | if not jobs: | 9 | for j in raw: |
| 10 | return 0 | 10 | if isinstance(j, (list, tuple)) and len(j) == 3: |
| 11 | 11 | jobs.append((j[1], j[0], j[2])) # sort by end, store as (end, start, weight) | |
| 12 | jobs = sorted(map(tuple, jobs), key=lambda x: (x[1], x[0], x[2])) | 12 | if not jobs: |
| 13 | ends = tuple(map(lambda x: x[1], jobs)) | 13 | return 0 |
| 14 | 14 | ||
| 15 | def br(a, x, hi): | 15 | jobs.sort() |
| 16 | lo = 0 | 16 | ends = [] |
| 17 | while lo < hi: | 17 | bests = [] |
| 18 | mid = (lo + hi) >> 1 | 18 | |
| 19 | if a[mid] <= x: | 19 | for e, s, w in jobs: |
| 20 | lo = mid + 1 | 20 | lo = 0 |
| 21 | else: | 21 | hi = len(ends) |
| 22 | hi = mid | 22 | while lo < hi: |
| 23 | return lo | 23 | mid = (lo + hi) >> 1 |
| 24 | 24 | if ends[mid] <= s: | |
| 25 | def step(state, job): | 25 | lo = mid + 1 |
| 26 | dp = state | 26 | else: |
| 27 | s, e, w = job | 27 | hi = mid |
| 28 | i = br(ends, s, len(dp)) | 28 | |
| 29 | take = w + dp[i] | 29 | candidate = w + (bests[lo - 1] if lo else 0) |
| 30 | best = take if take > dp[-1] else dp[-1] | 30 | current = bests[-1] if bests else 0 |
| 31 | return dp + (best,) | 31 | |
| 32 | 32 | if candidate > current: | |
| 33 | return __import__("functools").reduce(step, jobs, (0,))[-1] | 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 if not isinstance(input, dict):3 return 04 raw = input.get("jobs")5 if not raw:6 return 078 jobs = tuple(filter(lambda j: isinstance(j, (list, tuple)) and len(j) == 3, raw))9 if not jobs:10 return 01112 jobs = sorted(map(tuple, jobs), key=lambda x: (x[1], x[0], x[2]))13 ends = tuple(map(lambda x: x[1], jobs))1415 def br(a, x, hi):16 lo = 017 while lo < hi:18 mid = (lo + hi) >> 119 if a[mid] <= x:20 lo = mid + 121 else:22 hi = mid23 return lo2425 def step(state, job):26 dp = state27 s, e, w = job28 i = br(ends, s, len(dp))29 take = w + dp[i]30 best = take if take > dp[-1] else dp[-1]31 return dp + (best,)3233 return __import__("functools").reduce(step, jobs, (0,))[-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