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.
Move from brute-force thinking to an efficient approach using array strategy.
You are given a 2D integer array intervals where intervals[i] = [lefti, righti] represents the inclusive interval [lefti, righti].
You have to divide the intervals into one or more groups such that each interval is in exactly one group, and no two intervals that are in the same group intersect each other.
Return the minimum number of groups you need to make.
Two intervals intersect if there is at least one common number between them. For example, the intervals [1, 5] and [5, 8] intersect.
Example 1:
Input: intervals = [[5,10],[6,8],[1,5],[2,3],[1,10]] Output: 3 Explanation: We can divide the intervals into the following groups: - Group 1: [1, 5], [6, 8]. - Group 2: [2, 3], [5, 10]. - Group 3: [1, 10]. It can be proven that it is not possible to divide the intervals into fewer than 3 groups.
Example 2:
Input: intervals = [[1,3],[5,6],[8,10],[11,13]] Output: 1 Explanation: None of the intervals overlap, so we can put all of them in one group.
Constraints:
1 <= intervals.length <= 105intervals[i].length == 21 <= lefti <= righti <= 106Problem summary: You are given a 2D integer array intervals where intervals[i] = [lefti, righti] represents the inclusive interval [lefti, righti]. You have to divide the intervals into one or more groups such that each interval is in exactly one group, and no two intervals that are in the same group intersect each other. Return the minimum number of groups you need to make. Two intervals intersect if there is at least one common number between them. For example, the intervals [1, 5] and [5, 8] intersect.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Two Pointers · Greedy
[[5,10],[6,8],[1,5],[2,3],[1,10]]
[[1,3],[5,6],[8,10],[11,13]]
merge-intervals)minimum-number-of-frogs-croaking)average-height-of-buildings-in-each-segment)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2406: Divide Intervals Into Minimum Number of Groups
class Solution {
public int minGroups(int[][] intervals) {
Arrays.sort(intervals, (a, b) -> a[0] - b[0]);
PriorityQueue<Integer> q = new PriorityQueue<>();
for (var e : intervals) {
if (!q.isEmpty() && q.peek() < e[0]) {
q.poll();
}
q.offer(e[1]);
}
return q.size();
}
}
// Accepted solution for LeetCode #2406: Divide Intervals Into Minimum Number of Groups
func minGroups(intervals [][]int) int {
sort.Slice(intervals, func(i, j int) bool { return intervals[i][0] < intervals[j][0] })
q := hp{}
for _, e := range intervals {
if q.Len() > 0 && q.IntSlice[0] < e[0] {
heap.Pop(&q)
}
heap.Push(&q, e[1])
}
return q.Len()
}
type hp struct{ sort.IntSlice }
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
}
# Accepted solution for LeetCode #2406: Divide Intervals Into Minimum Number of Groups
class Solution:
def minGroups(self, intervals: List[List[int]]) -> int:
q = []
for left, right in sorted(intervals):
if q and q[0] < left:
heappop(q)
heappush(q, right)
return len(q)
// Accepted solution for LeetCode #2406: Divide Intervals Into Minimum Number of Groups
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2406: Divide Intervals Into Minimum Number of Groups
// class Solution {
// public int minGroups(int[][] intervals) {
// Arrays.sort(intervals, (a, b) -> a[0] - b[0]);
// PriorityQueue<Integer> q = new PriorityQueue<>();
// for (var e : intervals) {
// if (!q.isEmpty() && q.peek() < e[0]) {
// q.poll();
// }
// q.offer(e[1]);
// }
// return q.size();
// }
// }
// Accepted solution for LeetCode #2406: Divide Intervals Into Minimum Number of Groups
function minGroups(intervals: number[][]): number {
intervals.sort((a, b) => a[0] - b[0]);
const q = new PriorityQueue({ compare: (a, b) => a - b });
for (const [left, right] of intervals) {
if (!q.isEmpty() && q.front() < left) {
q.dequeue();
}
q.enqueue(right);
}
return q.size();
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair of elements. The outer loop picks one element, the inner loop scans the rest. For n elements that is n × (n−1)/2 comparisons = O(n²). No extra memory — just two loop variables.
Each pointer traverses the array at most once. With two pointers moving inward (or both moving right), the total number of steps is bounded by n. Each comparison is O(1), giving O(n) overall. No auxiliary data structures are needed — just two index variables.
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: Advancing both pointers shrinks the search space too aggressively and skips candidates.
Usually fails on: A valid pair can be skipped when only one side should move.
Fix: Move exactly one pointer per decision branch based on invariant.
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.