452. Minimum Number of Arrows to Burst Balloons
Tags:
Medium
Skills:
Interval
June 24, 2025
04:32 AM
No headings found
Loading content...
Related Posts
Leetcode
No headings found
Related Posts
Leetcode
Problem
Bài toán yêu cầu tìm số mũi tên tối thiểu cần bắn để làm nổ tất cả các quả bóng, với quả bóng là một đoạn [x_start, x_end] trên trục hoành. Một mũi tên bắn lên tại vị trí x sẽ làm nổ tất cả các bóng mà x nằm trong đoạn [x_start, x_end] của bóng đó
Example
1|---| : bóng 1 (1,6)
2 |-----| : bóng 2 (2,8)
3 |------| : bóng 4 (7,12)
4 |------| : bóng 3 (10,16)Approach
Cách làm
Time and space complexity
Solution
1function findMinArrowShots(points: number[][]): number {
2 if(points.length === 0) return 0;
3 points.sort((a, b) => a[1] - b[1]);
4
5 let arrows = 1;
6 let end = points[0][1];
7 for(let i =1 ; i < points.length; i++) {
8 if(points[i][0] > end) {
9 arrows++;
10 end = points[i][1];
11 }
12 }
13 return arrows;
14};arrows = 1, end là điểm kết thúc của bóng đầu tiênend thì cần mũi tên mới, cập nhật lại end Biến arrows thường được khởi tạo là 1 (hoặc tăng lên 1 khi gặp bóng đầu tiên) vì
arrows lên thêm 1 cho mũi tên mới