Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
We have n jobs, where every job is scheduled to be done from startTime[i] to endTime[i], obtaining a profit of profit[i].
You're given the startTime, endTime and profit arrays, return the maximum profit you can take such that there are no two jobs in the subset with overlapping time range.
If you choose a job that ends at time X you will be able to start another job that starts at time X.
Example 1:
Input: startTime = [1,2,3,3], endTime = [3,4,5,6], profit = [50,10,40,70] Output: 120 Explanation: The subset chosen is the first and fourth job. Time range [1-3]+[3-6] , we get profit of 120 = 50 + 70.
Example 2:
Input: startTime = [1,2,3,4,6], endTime = [3,5,10,6,9], profit = [20,20,100,70,60] Output: 150 Explanation: The subset chosen is the first, fourth and fifth job. Profit obtained 150 = 20 + 70 + 60.
Example 3:
Input: startTime = [1,1,1], endTime = [2,3,4], profit = [5,6,4] Output: 6
Constraints:
1 <= startTime.length == endTime.length == profit.length <= 5 * 1041 <= startTime[i] < endTime[i] <= 1091 <= profit[i] <= 104Problem summary: We have n jobs, where every job is scheduled to be done from startTime[i] to endTime[i], obtaining a profit of profit[i]. You're given the startTime, endTime and profit arrays, return the maximum profit you can take such that there are no two jobs in the subset with overlapping time range. If you choose a job that ends at time X you will be able to start another job that starts at time X.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Binary Search · Dynamic Programming
[1,2,3,3] [3,4,5,6] [50,10,40,70]
[1,2,3,4,6] [3,5,10,6,9] [20,20,100,70,60]
[1,1,1] [2,3,4] [5,6,4]
maximum-earnings-from-taxi)two-best-non-overlapping-events)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1235: Maximum Profit in Job Scheduling
class Solution {
private int[][] jobs;
private int[] f;
private int n;
public int jobScheduling(int[] startTime, int[] endTime, int[] profit) {
n = profit.length;
jobs = new int[n][3];
for (int i = 0; i < n; ++i) {
jobs[i] = new int[] {startTime[i], endTime[i], profit[i]};
}
Arrays.sort(jobs, (a, b) -> a[0] - b[0]);
f = new int[n];
return dfs(0);
}
private int dfs(int i) {
if (i >= n) {
return 0;
}
if (f[i] != 0) {
return f[i];
}
int e = jobs[i][1], p = jobs[i][2];
int j = search(jobs, e, i + 1);
int ans = Math.max(dfs(i + 1), p + dfs(j));
f[i] = ans;
return ans;
}
private int search(int[][] jobs, int x, int i) {
int left = i, right = n;
while (left < right) {
int mid = (left + right) >> 1;
if (jobs[mid][0] >= x) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
}
}
// Accepted solution for LeetCode #1235: Maximum Profit in Job Scheduling
func jobScheduling(startTime []int, endTime []int, profit []int) int {
n := len(profit)
type tuple struct{ s, e, p int }
jobs := make([]tuple, n)
for i, p := range profit {
jobs[i] = tuple{startTime[i], endTime[i], p}
}
sort.Slice(jobs, func(i, j int) bool { return jobs[i].s < jobs[j].s })
f := make([]int, n)
var dfs func(int) int
dfs = func(i int) int {
if i >= n {
return 0
}
if f[i] != 0 {
return f[i]
}
j := sort.Search(n, func(j int) bool { return jobs[j].s >= jobs[i].e })
ans := max(dfs(i+1), jobs[i].p+dfs(j))
f[i] = ans
return ans
}
return dfs(0)
}
# Accepted solution for LeetCode #1235: Maximum Profit in Job Scheduling
class Solution:
def jobScheduling(
self, startTime: List[int], endTime: List[int], profit: List[int]
) -> int:
@cache
def dfs(i):
if i >= n:
return 0
_, e, p = jobs[i]
j = bisect_left(jobs, e, lo=i + 1, key=lambda x: x[0])
return max(dfs(i + 1), p + dfs(j))
jobs = sorted(zip(startTime, endTime, profit))
n = len(profit)
return dfs(0)
// Accepted solution for LeetCode #1235: Maximum Profit in Job Scheduling
struct Solution;
use std::collections::BTreeMap;
impl Solution {
fn job_scheduling(start_time: Vec<i32>, end_time: Vec<i32>, profit: Vec<i32>) -> i32 {
let mut jobs = vec![];
let n = start_time.len();
for i in 0..n {
jobs.push((start_time[i], end_time[i], profit[i]));
}
jobs.sort_unstable();
let mut memo: BTreeMap<i32, i32> = BTreeMap::new();
let mut res = 0;
for i in (0..n).rev() {
let mut cur = jobs[i].2;
if let Some((_, val)) = memo.range(jobs[i].1..).next() {
cur += val;
}
res = res.max(cur);
memo.insert(jobs[i].0, res);
}
res
}
}
#[test]
fn test() {
let start_time = vec![1, 2, 3, 3];
let end_time = vec![3, 4, 5, 6];
let profit = vec![50, 10, 40, 70];
let res = 120;
assert_eq!(Solution::job_scheduling(start_time, end_time, profit), res);
let start_time = vec![1, 2, 3, 4, 6];
let end_time = vec![3, 5, 10, 6, 9];
let profit = vec![20, 20, 100, 70, 60];
let res = 150;
assert_eq!(Solution::job_scheduling(start_time, end_time, profit), res);
let start_time = vec![1, 1, 1];
let end_time = vec![2, 3, 4];
let profit = vec![5, 6, 4];
let res = 6;
assert_eq!(Solution::job_scheduling(start_time, end_time, profit), res);
}
// Accepted solution for LeetCode #1235: Maximum Profit in Job Scheduling
function jobScheduling(startTime: number[], endTime: number[], profit: number[]): number {
const n = startTime.length;
const f = new Array(n).fill(0);
const idx = new Array(n).fill(0).map((_, i) => i);
idx.sort((i, j) => startTime[i] - startTime[j]);
const search = (x: number) => {
let l = 0;
let r = n;
while (l < r) {
const mid = (l + r) >> 1;
if (startTime[idx[mid]] >= x) {
r = mid;
} else {
l = mid + 1;
}
}
return l;
};
const dfs = (i: number): number => {
if (i >= n) {
return 0;
}
if (f[i] !== 0) {
return f[i];
}
const j = search(endTime[idx[i]]);
return (f[i] = Math.max(dfs(i + 1), dfs(j) + profit[idx[i]]));
};
return dfs(0);
}
Use this to step through a reusable interview workflow for this problem.
Check every element from left to right until we find the target or exhaust the array. Each comparison is O(1), and we may visit all n elements, giving O(n). No extra space needed.
Each comparison eliminates half the remaining search space. After k comparisons, the space is n/2ᵏ. We stop when the space is 1, so k = log₂ n. No extra memory needed — just two pointers (lo, hi).
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.