Skip to content

Commit 2c92d3c

Browse files
committed
leetcode
1 parent 1dca2df commit 2c92d3c

File tree

4 files changed

+271
-0
lines changed

4 files changed

+271
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
3+
*- 452. Minimum Number of Arrows to Burst Balloons -*
4+
5+
6+
7+
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] = [x-start, x-end] denotes a balloon whose horizontal diameter stretches between x-start and x-end. You do not know the exact y-coordinates of the balloons.
8+
9+
Arrows can be shot up directly vertically (in the positive y-direction) from different points along the x-axis. A balloon with x-start and x-end is burst by an arrow shot at x if x-start <= x <= x-end. 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.
10+
11+
Given the array points, return the minimum number of arrows that must be shot to burst all balloons.
12+
13+
14+
15+
Example 1:
16+
17+
Input: points = [[10,16],[2,8],[1,6],[7,12]]
18+
Output: 2
19+
Explanation: The balloons can be burst by 2 arrows:
20+
- Shoot an arrow at x = 6, bursting the balloons [2,8] and [1,6].
21+
- Shoot an arrow at x = 11, bursting the balloons [10,16] and [7,12].
22+
Example 2:
23+
24+
Input: points = [[1,2],[3,4],[5,6],[7,8]]
25+
Output: 4
26+
Explanation: One arrow needs to be shot for each balloon for a total of 4 arrows.
27+
Example 3:
28+
29+
Input: points = [[1,2],[2,3],[3,4],[4,5]]
30+
Output: 2
31+
Explanation: The balloons can be burst by 2 arrows:
32+
- Shoot an arrow at x = 2, bursting the balloons [1,2] and [2,3].
33+
- Shoot an arrow at x = 4, bursting the balloons [3,4] and [4,5].
34+
35+
36+
Constraints:
37+
38+
1 <= points.length <= 105
39+
points[i].length == 2
40+
-231 <= x-start < x-end <= 231 - 1
41+
42+
43+
*/
44+
45+
import 'dart:math';
46+
47+
/*
48+
49+
50+
Idea:
51+
We know that eventually we have to shoot down every balloon, so for each ballon there must be an arrow whose position is between balloon[0] and balloon[1] inclusively. Given that, we can sort the array of balloons by their ending position. Then we make sure that while we take care of each balloon in order, we can shoot as many following balloons as possible.
52+
53+
So what position should we pick each time? We should shoot as to the right as possible, because since balloons are sorted, this gives you the best chance to take down more balloons. Therefore the position should always be balloon[i][1] for the ith balloon.
54+
55+
This is exactly what I do in the for loop: check how many balloons I can shoot down with one shot aiming at the ending position of the current balloon. Then I skip all these balloons and start again from the next one (or the leftmost remaining one) that needs another arrow.
56+
57+
Example:
58+
59+
balloons = [[7,10], [1,5], [3,6], [2,4], [1,4]]
60+
After sorting, it becomes:
61+
62+
balloons = [[2,4], [1,4], [1,5], [3,6], [7,10]]
63+
So first of all, we shoot at position 4, we go through the array and see that all first 4 balloons can be taken care of by this single shot. Then we need another shot for one last balloon. So the result should be 2.
64+
65+
66+
*/
67+
68+
class A {
69+
/**
70+
Two key Ideas :
71+
(a) Greedy heuristic : burst all the balloons whose start is <= min(end);
72+
(b) If the start > min(end), you need another arrow to burst it so increment arrows and move the end forward.
73+
**/
74+
int findMinArrowShots(List<List<int>> points) {
75+
if (points.length == 0) return 0;
76+
points.sort((a, b) => a[1] - b[1]);
77+
int arrowPos = points[0][1];
78+
int arrowCnt = 1;
79+
for (int i = 1; i < points.length; i++) {
80+
if (arrowPos >= points[i][0]) {
81+
continue;
82+
}
83+
arrowCnt++;
84+
arrowPos = points[i][1];
85+
}
86+
return arrowCnt;
87+
}
88+
}
89+
90+
class B {
91+
int findMinArrowShots(List<List<int>> points) {
92+
// corner case
93+
if (points.length == 0 || points[0].length == 0) return 0;
94+
// sort and get merger point
95+
points.sort((List<int> a, List<int> b) {
96+
if (a[0] != b[0]) {
97+
return a[0] - b[0];
98+
} else {
99+
return a[1] - b[1];
100+
}
101+
});
102+
103+
List<List<int>> res = [];
104+
for (List<int> mid in points) {
105+
//mid[0] and mid[1]
106+
if (res.length == 0) {
107+
res.add(mid);
108+
} else {
109+
List<int> temp = res[res.length - 1];
110+
if (mid[0] <= temp[1]) {
111+
// value equal is represent interact each other
112+
temp[0] = max(temp[0], mid[0]);
113+
temp[1] = min(temp[1], mid[1]);
114+
res[res.length - 1] = temp;
115+
} else {
116+
res.add(mid);
117+
}
118+
}
119+
}
120+
return res.length;
121+
}
122+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package main
2+
3+
import "sort"
4+
5+
// func findMinArrowShots(points [][]int) int {
6+
// if len(points) == 0 {
7+
// return 0
8+
// }
9+
10+
// sort.Slice(points, func(i, j int) bool {
11+
// return points[i][1] < points[j][1]
12+
// })
13+
// var arrowPosition int = points[0][1]
14+
// var arrowCount int = 1
15+
// for i := 1; i < len(points); i++ {
16+
// if arrowPosition >= points[i][0] {
17+
// continue
18+
// }
19+
// arrowCount++
20+
// arrowPosition = points[i][1]
21+
// }
22+
// return arrowCount
23+
24+
// }
25+
26+
func findMinArrowShots(points [][]int) int {
27+
// corner case
28+
if len(points) == 0 || len(points[0]) == 0 {
29+
return 0
30+
}
31+
32+
// sort and get merger point
33+
sort.Slice(points, func(i, j int) bool {
34+
if points[i][0] != points[j][0] {
35+
return points[i][0] < points[j][0]
36+
} else {
37+
return points[i][1] < points[j][1]
38+
}
39+
})
40+
41+
res := make([][]int, 0)
42+
for _, mid := range points {
43+
//mid[0] and mid[1]
44+
if len(res) == 0 {
45+
res = append(res, mid)
46+
} else {
47+
temp := res[len(res)-1]
48+
if mid[0] <= temp[1] {
49+
// value equal is represent interact each other
50+
temp[0] = max(temp[0], mid[0])
51+
temp[1] = min(temp[1], mid[1])
52+
res[len(res)-1] = temp
53+
} else {
54+
res = append(res, mid)
55+
}
56+
}
57+
}
58+
return len(res)
59+
}
60+
61+
func max(a, b int) int {
62+
if a > b {
63+
return a
64+
}
65+
return b
66+
}
67+
68+
func min(a, b int) int {
69+
if a < b {
70+
return a
71+
}
72+
return b
73+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# 🔥 2 Approaches 🔥 || Simple Fast and Easy || with Explanation
2+
3+
## Explanation
4+
5+
This problem actually asks us: how many intervals are left when you combine all possible intersections. Well, it get's a lot easier when we first sort the array by the starting points of the intervals. Due to sorting, we have a runtime complexity of
6+
**O(N log N)**.
7+
8+
Sort the array of intervals, starting with the smallest starting point.
9+
Store the current end of the interval.
10+
As long as the current end is bigger or equal the start of the interval, we don't need another arrow. Be careful here, we have to update the current end as it might happen that the end of this interval is smaller than our current one.
11+
If the start of the interval is bigger than our current end, we have to use a new arrow.
12+
13+
## Solution - 1 GREEDY
14+
15+
```dart
16+
class Solution {
17+
/**
18+
Two key Ideas :
19+
(a) Greedy heuristic : burst all the balloons whose start is <= min(end);
20+
(b) If the start > min(end), you need another arrow to burst it so increment arrows and move the end forward.
21+
**/
22+
int findMinArrowShots(List<List<int>> points) {
23+
if (points.length == 0) return 0;
24+
points.sort((a, b) => a[1] - b[1]);
25+
int arrowPos = points[0][1];
26+
int arrowCnt = 1;
27+
for (int i = 1; i < points.length; i++) {
28+
if (arrowPos >= points[i][0]) {
29+
continue;
30+
}
31+
arrowCnt++;
32+
arrowPos = points[i][1];
33+
}
34+
return arrowCnt;
35+
}
36+
}
37+
```
38+
39+
## Solution - 2 OPTIMIZED GREEDY
40+
41+
```dart
42+
class Solution {
43+
int findMinArrowShots(List<List<int>> points) {
44+
// corner case
45+
if (points.length == 0 || points[0].length == 0) return 0;
46+
// sort and get merger point
47+
points.sort((List<int> a, List<int> b) {
48+
if (a[0] != b[0]) {
49+
return a[0] - b[0];
50+
} else {
51+
return a[1] - b[1];
52+
}
53+
});
54+
55+
List<List<int>> res = [];
56+
for (List<int> mid in points) {
57+
//mid[0] and mid[1]
58+
if (res.length == 0) {
59+
res.add(mid);
60+
} else {
61+
List<int> temp = res[res.length - 1];
62+
if (mid[0] <= temp[1]) {
63+
// value equal is represent interact each other
64+
temp[0] = max(temp[0], mid[0]);
65+
temp[1] = min(temp[1], mid[1]);
66+
res[res.length - 1] = temp;
67+
} else {
68+
res.add(mid);
69+
}
70+
}
71+
}
72+
return res.length;
73+
}
74+
}
75+
```

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
177177
- [**520.** Detect Capital](DetectCapital/detect_capital.dart)
178178
- [**944.** Delete Columns to Make Sorted](DeleteColumnsToMakeSorted/delete_columns_to_make_sorted.dart)
179179
- [**2244.** Minimum Rounds to Complete All Tasks](MinimumRoundsToCompleteAllTasks/minimum_rounds_to_complete_all_tasks.dart)
180+
- [**452.** Minimum Number of Arrows to Burst Balloons](MinimumNumberOfArrowsToBurstBalloons/minimum_number_of_arrows_to_burst_balloons.dart)
180181

181182
## Reach me via
182183

0 commit comments

Comments
 (0)