Skip to content

Commit f8f7b09

Browse files
committed
October leetcoding challenge, week 2, Q 3
1 parent 974cb01 commit f8f7b09

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package competitiveProgramming.leetcode.thirtyDaysLeetcodingChallenge.october.week2;
2+
3+
import utils.ArrayUtils;
4+
5+
import java.util.Arrays;
6+
import java.util.Comparator;
7+
import java.util.Stack;
8+
9+
/*
10+
https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/560/week-2-october-8th-october-14th/3490/
11+
12+
Minimum Number of Arrows to Burst Balloons
13+
14+
There are some spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it's horizontal, y-coordinates don't matter, and hence the x-coordinates of start and end of the diameter suffice. The start is always smaller than the end.
15+
16+
An arrow can be shot up exactly vertically from different points along the x-axis. A balloon with xstart and xend bursts by an arrow shot at x if xstart ≤ x ≤ xend. There is no limit to the number of arrows that can be shot. An arrow once shot keeps traveling up infinitely.
17+
18+
Given an array points where points[i] = [xstart, xend], return the minimum number of arrows that must be shot to burst all balloons.
19+
20+
21+
22+
Example 1:
23+
24+
Input: points = [[10,16],[2,8],[1,6],[7,12]]
25+
Output: 2
26+
Explanation: One way is to shoot one arrow for example at x = 6 (bursting the balloons [2,8] and [1,6]) and another arrow at x = 11 (bursting the other two balloons).
27+
28+
Example 2:
29+
30+
Input: points = [[1,2],[3,4],[5,6],[7,8]]
31+
Output: 4
32+
33+
Example 3:
34+
35+
Input: points = [[1,2],[2,3],[3,4],[4,5]]
36+
Output: 2
37+
38+
Example 4:
39+
40+
Input: points = [[1,2]]
41+
Output: 1
42+
43+
Example 5:
44+
45+
Input: points = [[2,3],[2,3]]
46+
Output: 1
47+
48+
49+
50+
Constraints:
51+
52+
0 <= points.length <= 104
53+
points.length == 2
54+
-231 <= xstart < xend <= 231 - 1
55+
56+
*/
57+
public class MinimumNumberOfArrowsToBurst {
58+
public static void main(String[] args) {
59+
System.out.println(findMinArrowShots(ArrayUtils.parse2DArray("[[10,16],[2,8],[1,6],[7,12]]")));
60+
System.out.println(findMinArrowShots(ArrayUtils.parse2DArray("[[1,2],[3,4],[5,6],[7,8]]")));
61+
System.out.println(findMinArrowShots(ArrayUtils.parse2DArray("[[1,2],[2,3],[3,4],[4,5]]")));
62+
System.out.println(findMinArrowShots(ArrayUtils.parse2DArray("[[1,2]")));
63+
System.out.println(findMinArrowShots(ArrayUtils.parse2DArray("[[2,3],[2,3]]")));
64+
}
65+
66+
public static int findMinArrowShots(int[][] points) {
67+
if(points == null || points.length == 0) return 0;
68+
69+
Stack<int[]> stack = new Stack<>();
70+
int count = 1;
71+
int i = 1;
72+
73+
Arrays.sort(points, Comparator.comparingInt(a -> a[1]));
74+
stack.push(points[0]);
75+
76+
while (i < points.length) {
77+
int[] prev = stack.peek();
78+
int[] current = points[i];
79+
80+
if(prev[1] >= current[0]) { //intersection => continue
81+
stack.push(new int[]{current[0], prev[1]});
82+
} else {
83+
stack.push(current);
84+
count++;
85+
}
86+
i++;
87+
}
88+
89+
return count;
90+
}
91+
}

0 commit comments

Comments
 (0)