Solution #c90b917a-c0e6-41a7-9b36-81bb36459e9d
completedScore
80% (4/5)
Runtime
15μs
Delta
-20.0% vs best
First in chain
Score
80% (4/5)
Runtime
15μs
Delta
-20.0% vs best
First in chain
def solve(input):
jobs = input["jobs"]
# Sort jobs based on their end time
jobs.sort(key=lambda job: job[1])
# Function to perform binary search to find the last non-conflicting job
def find_last_non_conflicting(jobs, index):
low, high = 0, index - 1
while low <= high:
mid = (low + high) // 2
if jobs[mid][1] <= jobs[index][0]:
if jobs[mid + 1][1] <= jobs[index][0]:
low = mid + 1
else:
return mid
else:
high = mid - 1
return -1
# Dynamic programming table to store the maximum weight up to each job
n = len(jobs)
dp = [0] * n
dp[0] = jobs[0][2]
for i in range(1, n):
# Include the current job
include_weight = jobs[i][2]
last_non_conflicting = find_last_non_conflicting(jobs, i)
if last_non_conflicting != -1:
include_weight += dp[last_non_conflicting]
# Exclude the current job
exclude_weight = dp[i - 1]
# Take the maximum of including or excluding the current job
dp[i] = max(include_weight, exclude_weight)
# The last entry in dp table will have the result
return dp[-1]Score Difference
-20.0%
Runtime Advantage
3μs faster
Code Size
39 vs 39 lines
| # | Your Solution | # | Champion |
|---|---|---|---|
| 1 | def solve(input): | 1 | def solve(input): |
| 2 | jobs = input["jobs"] | 2 | if not isinstance(input, dict): |
| 3 | # Sort jobs based on their end time | 3 | return 0 |
| 4 | jobs.sort(key=lambda job: job[1]) | 4 | raw = input.get("jobs") |
| 5 | 5 | if not raw: | |
| 6 | # Function to perform binary search to find the last non-conflicting job | 6 | return 0 |
| 7 | def find_last_non_conflicting(jobs, index): | 7 | |
| 8 | low, high = 0, index - 1 | 8 | jobs = [] |
| 9 | while low <= high: | 9 | for j in raw: |
| 10 | mid = (low + high) // 2 | 10 | if isinstance(j, (list, tuple)) and len(j) == 3: |
| 11 | if jobs[mid][1] <= jobs[index][0]: | 11 | jobs.append((j[1], j[0], j[2])) # sort by end, store as (end, start, weight) |
| 12 | if jobs[mid + 1][1] <= jobs[index][0]: | 12 | if not jobs: |
| 13 | low = mid + 1 | 13 | return 0 |
| 14 | else: | 14 | |
| 15 | return mid | 15 | jobs.sort() |
| 16 | else: | 16 | ends = [] |
| 17 | high = mid - 1 | 17 | bests = [] |
| 18 | return -1 | 18 | |
| 19 | 19 | for e, s, w in jobs: | |
| 20 | # Dynamic programming table to store the maximum weight up to each job | 20 | lo = 0 |
| 21 | n = len(jobs) | 21 | hi = len(ends) |
| 22 | dp = [0] * n | 22 | while lo < hi: |
| 23 | dp[0] = jobs[0][2] | 23 | mid = (lo + hi) >> 1 |
| 24 | 24 | if ends[mid] <= s: | |
| 25 | for i in range(1, n): | 25 | lo = mid + 1 |
| 26 | # Include the current job | 26 | else: |
| 27 | include_weight = jobs[i][2] | 27 | hi = mid |
| 28 | last_non_conflicting = find_last_non_conflicting(jobs, i) | 28 | |
| 29 | if last_non_conflicting != -1: | 29 | candidate = w + (bests[lo - 1] if lo else 0) |
| 30 | include_weight += dp[last_non_conflicting] | 30 | current = bests[-1] if bests else 0 |
| 31 | 31 | ||
| 32 | # Exclude the current job | 32 | if candidate > current: |
| 33 | exclude_weight = dp[i - 1] | 33 | if ends and ends[-1] == e: |
| 34 | 34 | bests[-1] = candidate | |
| 35 | # Take the maximum of including or excluding the current job | 35 | else: |
| 36 | dp[i] = max(include_weight, exclude_weight) | 36 | ends.append(e) |
| 37 | 37 | bests.append(candidate) | |
| 38 | # The last entry in dp table will have the result | 38 | |
| 39 | return dp[-1] | 39 | return bests[-1] if bests else 0 |
1def solve(input):2 jobs = input["jobs"]3 # Sort jobs based on their end time4 jobs.sort(key=lambda job: job[1])56 # Function to perform binary search to find the last non-conflicting job7 def find_last_non_conflicting(jobs, index):8 low, high = 0, index - 19 while low <= high:10 mid = (low + high) // 211 if jobs[mid][1] <= jobs[index][0]:12 if jobs[mid + 1][1] <= jobs[index][0]:13 low = mid + 114 else:15 return mid16 else:17 high = mid - 118 return -11920 # Dynamic programming table to store the maximum weight up to each job21 n = len(jobs)22 dp = [0] * n23 dp[0] = jobs[0][2]2425 for i in range(1, n):26 # Include the current job27 include_weight = jobs[i][2]28 last_non_conflicting = find_last_non_conflicting(jobs, i)29 if last_non_conflicting != -1:30 include_weight += dp[last_non_conflicting]3132 # Exclude the current job33 exclude_weight = dp[i - 1]3435 # Take the maximum of including or excluding the current job36 dp[i] = max(include_weight, exclude_weight)3738 # The last entry in dp table will have the result39 return dp[-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