Weighted interval scheduling
Return the maximum reward from nonoverlapping positive-length jobs (start,end,reward). Jobs are half-open, rewards nonnegative, and touching endpoints are compatible.
from bisect import bisect_right
ends = [3, 5, 6]
start = 5
print(bisect_right(ends, start))Reason about the state
Sort jobs by end time. Binary-search the last compatible prefix and compare taking the job with skipping it, giving O(n log n) time. Before writing code, identify what each variable means after one iteration. Walk through the example and explain why the next transition preserves that meaning. Then check the smallest permitted input and a case with repeated values. A correct answer follows the contract for every input, including the boundary cases in the checks. Keep any input mutation within the stated rules; return a fresh result when the task asks for one.
Your task
Return the maximum reward from nonoverlapping positive-length jobs (start,end,reward). Jobs are half-open, rewards nonnegative, and touching endpoints are compatible.
The explanation opens here once every check passes.