Skip to content

Commit 82a298e

Browse files
add a solution for 452
1 parent 5a98d2c commit 82a298e

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,31 @@ public int findMinArrowShots(int[][] points) {
5454
return minArrows;
5555
}
5656
}
57+
58+
public static class Solution3 {
59+
/**
60+
* Another approach of mine: completely original.
61+
* 1. Sort the points by start first, if tie, sort by end, both ascendingly.
62+
* 2. While checking, we'll keep updating the ending to be the smaller one so that we don't possibly miss to burst a balloon. See test case 4 for this class.
63+
*/
64+
65+
public int findMinArrowShots(int[][] points) {
66+
int arrowShots = 0;
67+
Arrays.sort(points, (a, b) -> a[0] != b[0] ? Integer.compare(a[0], b[0]) : Integer.compare(a[1], b[1]));
68+
for (int i = 0; i < points.length; ) {
69+
int end = points[i][1];
70+
int j = i + 1;
71+
for (; j < points.length; j++) {
72+
if (points[j][0] <= end) {
73+
end = Math.min(end, points[j][1]);
74+
} else {
75+
break;
76+
}
77+
}
78+
i = j;
79+
arrowShots++;
80+
}
81+
return arrowShots;
82+
}
83+
}
5784
}

src/test/java/com/fishercoder/_452Test.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
public class _452Test {
1111
private static _452.Solution1 solution1;
1212
private static _452.Solution2 solution2;
13+
private static _452.Solution3 solution3;
1314

1415
@BeforeClass
1516
public static void setup() {
1617
solution1 = new _452.Solution1();
1718
solution2 = new _452.Solution2();
19+
solution3 = new _452.Solution3();
1820
}
1921

2022
@Test
@@ -23,6 +25,7 @@ public void test1() {
2325
("[3,9],[7,12],[3,8],[6,8],[9,10],[2,9],[0,9],[3,9],[0,6],[2,8]");
2426
assertEquals(2, solution1.findMinArrowShots(points));
2527
assertEquals(2, solution2.findMinArrowShots(points));
28+
assertEquals(2, solution3.findMinArrowShots(points));
2629
}
2730

2831
@Test
@@ -31,6 +34,7 @@ public void test2() {
3134
("[-2147483646,-2147483645],[2147483646,2147483647]");
3235
assertEquals(2, solution1.findMinArrowShots(points));
3336
assertEquals(2, solution2.findMinArrowShots(points));
37+
assertEquals(2, solution3.findMinArrowShots(points));
3438
}
3539

3640
@Test
@@ -39,6 +43,16 @@ public void test3() {
3943
("[0,6],[0,9],[7,8]");
4044
assertEquals(2, solution1.findMinArrowShots(points));
4145
assertEquals(2, solution2.findMinArrowShots(points));
46+
assertEquals(2, solution3.findMinArrowShots(points));
47+
}
48+
49+
@Test
50+
public void test4() {
51+
int[][] points = CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray
52+
("[9,12],[1,10],[4,11],[8,12],[3,9],[6,9],[6,7]");
53+
assertEquals(2, solution1.findMinArrowShots(points));
54+
assertEquals(2, solution2.findMinArrowShots(points));
55+
assertEquals(2, solution3.findMinArrowShots(points));
4256
}
4357

4458
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy