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.
There are n different online courses numbered from 1 to n. You are given an array courses where courses[i] = [durationi, lastDayi] indicate that the ith course should be taken continuously for durationi days and must be finished before or on lastDayi.
You will start on the 1st day and you cannot take two or more courses simultaneously.
Return the maximum number of courses that you can take.
Example 1:
Input: courses = [[100,200],[200,1300],[1000,1250],[2000,3200]] Output: 3 Explanation: There are totally 4 courses, but you can take 3 courses at most: First, take the 1st course, it costs 100 days so you will finish it on the 100th day, and ready to take the next course on the 101st day. Second, take the 3rd course, it costs 1000 days so you will finish it on the 1100th day, and ready to take the next course on the 1101st day. Third, take the 2nd course, it costs 200 days so you will finish it on the 1300th day. The 4th course cannot be taken now, since you will finish it on the 3300th day, which exceeds the closed date.
Example 2:
Input: courses = [[1,2]] Output: 1
Example 3:
Input: courses = [[3,2],[4,3]] Output: 0
Constraints:
1 <= courses.length <= 1041 <= durationi, lastDayi <= 104Problem summary: There are n different online courses numbered from 1 to n. You are given an array courses where courses[i] = [durationi, lastDayi] indicate that the ith course should be taken continuously for durationi days and must be finished before or on lastDayi. You will start on the 1st day and you cannot take two or more courses simultaneously. Return the maximum number of courses that you can take.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Greedy
[[100,200],[200,1300],[1000,1250],[2000,3200]]
[[1,2]]
[[3,2],[4,3]]
course-schedule)course-schedule-ii)parallel-courses-iii)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #630: Course Schedule III
class Solution {
public int scheduleCourse(int[][] courses) {
Arrays.sort(courses, (a, b) -> a[1] - b[1]);
PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> b - a);
int s = 0;
for (var e : courses) {
int duration = e[0], last = e[1];
pq.offer(duration);
s += duration;
while (s > last) {
s -= pq.poll();
}
}
return pq.size();
}
}
// Accepted solution for LeetCode #630: Course Schedule III
func scheduleCourse(courses [][]int) int {
sort.Slice(courses, func(i, j int) bool { return courses[i][1] < courses[j][1] })
pq := &hp{}
s := 0
for _, e := range courses {
duration, last := e[0], e[1]
s += duration
pq.push(duration)
for s > last {
s -= pq.pop()
}
}
return pq.Len()
}
type hp struct{ sort.IntSlice }
func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] }
func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) }
func (h *hp) Pop() any {
a := h.IntSlice
v := a[len(a)-1]
h.IntSlice = a[:len(a)-1]
return v
}
func (h *hp) push(v int) { heap.Push(h, v) }
func (h *hp) pop() int { return heap.Pop(h).(int) }
# Accepted solution for LeetCode #630: Course Schedule III
class Solution:
def scheduleCourse(self, courses: List[List[int]]) -> int:
courses.sort(key=lambda x: x[1])
pq = []
s = 0
for duration, last in courses:
heappush(pq, -duration)
s += duration
while s > last:
s += heappop(pq)
return len(pq)
// Accepted solution for LeetCode #630: Course Schedule III
struct Solution;
use std::collections::BinaryHeap;
impl Solution {
fn schedule_course(mut courses: Vec<Vec<i32>>) -> i32 {
courses.sort_by_key(|x| x[1]);
let mut sum = 0;
let mut queue: BinaryHeap<i32> = BinaryHeap::new();
for c in courses {
sum += c[0];
queue.push(c[0]);
if sum > c[1] {
sum -= queue.pop().unwrap();
}
}
queue.len() as i32
}
}
#[test]
fn test() {
let courses = vec_vec_i32![[100, 200], [200, 1300], [1000, 1250], [2000, 3200]];
let res = 3;
assert_eq!(Solution::schedule_course(courses), res);
}
// Accepted solution for LeetCode #630: Course Schedule III
function scheduleCourse(courses: number[][]): number {
courses.sort((a, b) => a[1] - b[1]);
const pq = new MaxPriorityQueue<number>();
let s = 0;
for (const [duration, last] of courses) {
pq.enqueue(duration);
s += duration;
while (s > last) {
s -= pq.dequeue();
}
}
return pq.size();
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
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: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.