Solution #58b73178-c67d-47f9-bd28-6d8c33fe6b26
completedScore
100% (5/5)
Runtime
54μs
Delta
No change vs parent
Tied for best
Same as parent
Score
100% (5/5)
Runtime
54μ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
items = tuple(sorted(map(lambda j: (j[1], j[0], j[2]), jobs)))
ends = tuple(map(lambda x: x[0], items))
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
def rec(i, memo={}):
if i <= 0:
return 0
if i in memo:
return memo[i]
e, s, w = items[i - 1]
j = bisect_right(ends, s, 0, i - 1)
a = rec(i - 1, memo)
b = w + rec(j, memo)
memo[i] = a if a > b else b
return memo[i]
return rec(len(items), {})Score Difference
Tied
Runtime Advantage
36μs slower
Code Size
41 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 | items = tuple(sorted(map(lambda j: (j[1], j[0], j[2]), jobs))) | 12 | if not jobs: |
| 13 | ends = tuple(map(lambda x: x[0], items)) | 13 | return 0 |
| 14 | 14 | ||
| 15 | try: | 15 | jobs.sort() |
| 16 | from bisect import bisect_right | 16 | ends = [] |
| 17 | except Exception: | 17 | bests = [] |
| 18 | def bisect_right(a, x, lo=0, hi=None): | 18 | |
| 19 | if hi is None: | 19 | for e, s, w in jobs: |
| 20 | hi = len(a) | 20 | lo = 0 |
| 21 | while lo < hi: | 21 | hi = len(ends) |
| 22 | mid = (lo + hi) >> 1 | 22 | while lo < hi: |
| 23 | if x < a[mid]: | 23 | mid = (lo + hi) >> 1 |
| 24 | hi = mid | 24 | if ends[mid] <= s: |
| 25 | else: | 25 | lo = mid + 1 |
| 26 | lo = mid + 1 | 26 | else: |
| 27 | return lo | 27 | hi = mid |
| 28 | 28 | ||
| 29 | def rec(i, memo={}): | 29 | candidate = w + (bests[lo - 1] if lo else 0) |
| 30 | if i <= 0: | 30 | current = bests[-1] if bests else 0 |
| 31 | return 0 | 31 | |
| 32 | if i in memo: | 32 | if candidate > current: |
| 33 | return memo[i] | 33 | if ends and ends[-1] == e: |
| 34 | e, s, w = items[i - 1] | 34 | bests[-1] = candidate |
| 35 | j = bisect_right(ends, s, 0, i - 1) | 35 | else: |
| 36 | a = rec(i - 1, memo) | 36 | ends.append(e) |
| 37 | b = w + rec(j, memo) | 37 | bests.append(candidate) |
| 38 | memo[i] = a if a > b else b | 38 | |
| 39 | return memo[i] | 39 | return bests[-1] if bests else 0 |
| 40 | 40 | ||
| 41 | return rec(len(items), {}) | 41 |
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 items = tuple(sorted(map(lambda j: (j[1], j[0], j[2]), jobs)))13 ends = tuple(map(lambda x: x[0], items))1415 try:16 from bisect import bisect_right17 except Exception:18 def bisect_right(a, x, lo=0, hi=None):19 if hi is None:20 hi = len(a)21 while lo < hi:22 mid = (lo + hi) >> 123 if x < a[mid]:24 hi = mid25 else:26 lo = mid + 127 return lo2829 def rec(i, memo={}):30 if i <= 0:31 return 032 if i in memo:33 return memo[i]34 e, s, w = items[i - 1]35 j = bisect_right(ends, s, 0, i - 1)36 a = rec(i - 1, memo)37 b = w + rec(j, memo)38 memo[i] = a if a > b else b39 return memo[i]4041 return rec(len(items), {})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