Non-overlapping Intervals
Return minimum number of intervals to remove to make rest non-overlapping Intervals touching at a point are non-overlapping: [1,2] and [2,3] Example: [[1,2],[2,3],[3,4],[1,3]] => 1 (remove [1,3])
Non-overlapping Intervals is a dynamic programming question: define the state cleanly and the transitions usually follow. Interviewers love it because it reveals your state-and-transition thinking and your edge-case discipline. The trap is picking a state that's too vague; keep it minimal and consistent.
Examples
You write
erase_overlap_intervals(intervals: List[List[int]])
Example 1
- Input
- [[1,2],[2,3],[3,4],[1,3]]
- returns
- 1
Example 2
- Input
- [[1,2],[1,2],[1,2]]
- returns
- 2
The pattern behind it
Arrays & Hashing
I reach for it when
- You catch yourself scanning the array again for every element.
- The problem asks about duplicates, pairs, counts or groups.
Say this before you type. Say what the key is and what the value remembers, before you write the loop.
All 127 Arrays & Hashing problemsDynamic Programming
I reach for it when
- Brute force repeats the same subproblem.
- You are asked for a count, a minimum or a maximum.
Say this before you type. Say the state in one sentence, then say which smaller states it reads from.
All 37 Dynamic Programming problems
Meet Coco
Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.