-
Notifications
You must be signed in to change notification settings - Fork 13
/
Arrays_MinNumOfArrowsToBurstBalloons.java
59 lines (50 loc) · 2.51 KB
/
Arrays_MinNumOfArrowsToBurstBalloons.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
// https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/
// https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/discuss/93703/Share-my-explained-Greedy-solution (BEST Resource and explanation)
// https://www.youtube.com/watch?v=FEM7uVzSbYI&t=342s&ab_channel=HappyCoding (A good video to understand)
// https://www.youtube.com/watch?v=y_n1rALjKzI&ab_channel=bitwisebytes (A good video for visualization)
public class Arrays_MinNumOfArrowsToBurstBalloons {
/*LOGIC :
* We sort all the balloons position on the basis of ending position.
* And then run over the loop to find out how many balloons I can take down with one shot by aiming at the ending position of the current balloon.
* whenever we get to see a balloon, we check the range of it and
* if the current balloon's start time is in the range, then
* we don't increase the arrow count
* as this balloon can be burst down by current arrow.*/
private static int calculateMinNumberOfArrowsToBurstBalloons(int[][] balloons) {
if (balloons.length == 0) return 0;
Arrays.sort(balloons, (a, b) -> (a[1] - b[1])); /* sorted by the end coordinate */
int numOfArrows = 1;
int currentEnding = balloons[0][1];
for (int i = 1; i < balloons.length; i++) {
int currentStart = balloons[i][0];
if (currentStart <= currentEnding) {
continue;
}
else {
numOfArrows++;
currentEnding = balloons[i][1];
}
}
return numOfArrows;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine().trim());
while (t -- > 0) {
int numOfBalloons = Integer.parseInt(br.readLine().trim());
int[][] balloons = new int[numOfBalloons][2];
for (int i = 0; i < numOfBalloons; i++) {
String[] coordinates = br.readLine().trim().split("\\s+");
int startCoordinate = Integer.parseInt(coordinates[0]);
int endCoordinate = Integer.parseInt(coordinates[1]);
balloons[i][0] = startCoordinate;
balloons[i][1] = endCoordinate;
}
System.out.println(calculateMinNumberOfArrowsToBurstBalloons(balloons));
}
}
}