Skip to content

Commit 60a5608

Browse files
add a solution for 452
1 parent 3a2a84e commit 60a5608

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

src/main/java/com/fishercoder/solutions/_452.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ public int findMinArrowShots(int[][] points) {
1717
int currentEnd = points[0][1];
1818
int count = 1;
1919
for (int[] p : points) {
20-
// if the point starts after currentEnd, it means this balloons not been bursted. Then we shot the balloon in its end point. Otherwise, means this balloon has been bursted, then ignore it.
20+
// if the point starts after currentEnd, it means this balloons not been burst.
21+
// Then we shot the balloon in its end point. Otherwise, means this balloon has been burst, then ignore it.
2122
if (p[0] > currentEnd) {
2223
count++;
2324
currentEnd = p[1];
@@ -28,4 +29,29 @@ public int findMinArrowShots(int[][] points) {
2829
return count;
2930
}
3031
}
32+
33+
public static class Solution2 {
34+
/**
35+
* I'm glad to have come up with this solution on my own on 10/13/2021:
36+
* we'll have to sort the balloons by its ending points, a counter case to this is below:
37+
* {{0, 6}, {0, 9}, {7, 8}}
38+
* if we sort by starting points, then it becomes: {0, 6}, {0, 9}, {7, 8}, this way, if we shoot 9, {0, 6} won't be burst
39+
* however, if we sort by ending points, then it becomes: {0, 6}, {7, 8}, {0, 9},
40+
* then we shoot at 6, then at 8, this gives us the result of bursting all balloons.
41+
*/
42+
public int findMinArrowShots(int[][] points) {
43+
Arrays.sort(points, (a, b) -> Integer.compare(a[1], b[1]));
44+
int minArrows = 1;
45+
long end = points[0][1];
46+
for (int i = 1; i < points.length; i++) {
47+
if (points[i][0] <= end) {
48+
continue;
49+
} else {
50+
minArrows++;
51+
end = points[i][1];
52+
}
53+
}
54+
return minArrows;
55+
}
56+
}
3157
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.utils.CommonUtils;
4+
import com.fishercoder.solutions._452;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class _452Test {
11+
private static _452.Solution1 solution1;
12+
private static _452.Solution2 solution2;
13+
14+
@BeforeClass
15+
public static void setup() {
16+
solution1 = new _452.Solution1();
17+
solution2 = new _452.Solution2();
18+
}
19+
20+
@Test
21+
public void test1() {
22+
int[][] points = CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray
23+
("[3,9],[7,12],[3,8],[6,8],[9,10],[2,9],[0,9],[3,9],[0,6],[2,8]");
24+
assertEquals(2, solution1.findMinArrowShots(points));
25+
assertEquals(2, solution2.findMinArrowShots(points));
26+
}
27+
28+
@Test
29+
public void test2() {
30+
int[][] points = CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray
31+
("[-2147483646,-2147483645],[2147483646,2147483647]");
32+
assertEquals(2, solution1.findMinArrowShots(points));
33+
assertEquals(2, solution2.findMinArrowShots(points));
34+
}
35+
36+
@Test
37+
public void test3() {
38+
int[][] points = CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray
39+
("[0,6],[0,9],[7,8]");
40+
assertEquals(2, solution1.findMinArrowShots(points));
41+
assertEquals(2, solution2.findMinArrowShots(points));
42+
}
43+
44+
}

0 commit comments

Comments
 (0)