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.
There are some spherical balloons taped onto a flat wall that represents the XY-plane. The balloons are represented as a 2D integer array points where points[i] = [xstart, xend] denotes a balloon whose horizontal diameter stretches between xstart and xend. You do not know the exact y-coordinates of the balloons.
Arrows can be shot up directly vertically (in the positive y-direction) from different points along the x-axis. A balloon with xstart and xend is burst by an arrow shot at x if xstart <= x <= xend. There is no limit to the number of arrows that can be shot. A shot arrow keeps traveling up infinitely, bursting any balloons in its path.
Given the array points, return the minimum number of arrows that must be shot to burst all balloons.
Example 1:
Input: points = [[10,16],[2,8],[1,6],[7,12]] Output: 2 Explanation: The balloons can be burst by 2 arrows: - Shoot an arrow at x = 6, bursting the balloons [2,8] and [1,6]. - Shoot an arrow at x = 11, bursting the balloons [10,16] and [7,12].
Example 2:
Input: points = [[1,2],[3,4],[5,6],[7,8]] Output: 4 Explanation: One arrow needs to be shot for each balloon for a total of 4 arrows.
Example 3:
Input: points = [[1,2],[2,3],[3,4],[4,5]] Output: 2 Explanation: The balloons can be burst by 2 arrows: - Shoot an arrow at x = 2, bursting the balloons [1,2] and [2,3]. - Shoot an arrow at x = 4, bursting the balloons [3,4] and [4,5].
Constraints:
1 <= points.length <= 105points[i].length == 2-231 <= xstart < xend <= 231 - 1Problem summary: There are some spherical balloons taped onto a flat wall that represents the XY-plane. The balloons are represented as a 2D integer array points where points[i] = [xstart, xend] denotes a balloon whose horizontal diameter stretches between xstart and xend. You do not know the exact y-coordinates of the balloons. Arrows can be shot up directly vertically (in the positive y-direction) from different points along the x-axis. A balloon with xstart and xend is burst by an arrow shot at x if xstart <= x <= xend. There is no limit to the number of arrows that can be shot. A shot arrow keeps traveling up infinitely, bursting any balloons in its path. Given the array points, return the minimum number of arrows that must be shot to burst all balloons.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Greedy
[[10,16],[2,8],[1,6],[7,12]]
[[1,2],[3,4],[5,6],[7,8]]
[[1,2],[2,3],[3,4],[4,5]]
meeting-rooms-ii)non-overlapping-intervals)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #452: Minimum Number of Arrows to Burst Balloons
class Solution {
public int findMinArrowShots(int[][] points) {
// 直接 a[1] - b[1] 可能会溢出
Arrays.sort(points, Comparator.comparingInt(a -> a[1]));
int ans = 0;
long last = -(1L << 60);
for (var p : points) {
int a = p[0], b = p[1];
if (a > last) {
++ans;
last = b;
}
}
return ans;
}
}
// Accepted solution for LeetCode #452: Minimum Number of Arrows to Burst Balloons
func findMinArrowShots(points [][]int) (ans int) {
sort.Slice(points, func(i, j int) bool { return points[i][1] < points[j][1] })
last := -(1 << 60)
for _, p := range points {
a, b := p[0], p[1]
if a > last {
ans++
last = b
}
}
return
}
# Accepted solution for LeetCode #452: Minimum Number of Arrows to Burst Balloons
class Solution:
def findMinArrowShots(self, points: List[List[int]]) -> int:
ans, last = 0, -inf
for a, b in sorted(points, key=lambda x: x[1]):
if a > last:
ans += 1
last = b
return ans
// Accepted solution for LeetCode #452: Minimum Number of Arrows to Burst Balloons
struct Solution;
impl Solution {
fn find_min_arrow_shots(mut points: Vec<Vec<i32>>) -> i32 {
let n = points.len();
if n == 0 {
return 0;
}
points.sort_by_key(|p| p[1]);
let mut end = points[0][1];
let mut res = 1;
for i in 1..n {
if points[i][0] <= end {
continue;
}
end = points[i][1];
res += 1;
}
res
}
}
#[test]
fn test() {
let points = vec_vec_i32![[10, 16], [2, 8], [1, 6], [7, 12]];
let res = 2;
assert_eq!(Solution::find_min_arrow_shots(points), res);
}
// Accepted solution for LeetCode #452: Minimum Number of Arrows to Burst Balloons
function findMinArrowShots(points: number[][]): number {
points.sort((a, b) => a[1] - b[1]);
let ans = 0;
let last = -Infinity;
for (const [a, b] of points) {
if (last < a) {
ans++;
last = b;
}
}
return ans;
}
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.