Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit60a5608

Browse files
add a solution for 452
1 parent3a2a84e commit60a5608

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
intcurrentEnd =points[0][1];
1818
intcount =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
returncount;
2930
}
3031
}
32+
33+
publicstaticclassSolution2 {
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+
publicintfindMinArrowShots(int[][]points) {
43+
Arrays.sort(points, (a,b) ->Integer.compare(a[1],b[1]));
44+
intminArrows =1;
45+
longend =points[0][1];
46+
for (inti =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+
returnminArrows;
55+
}
56+
}
3157
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.common.utils.CommonUtils;
4+
importcom.fishercoder.solutions._452;
5+
importorg.junit.BeforeClass;
6+
importorg.junit.Test;
7+
8+
importstaticorg.junit.Assert.assertEquals;
9+
10+
publicclass_452Test {
11+
privatestatic_452.Solution1solution1;
12+
privatestatic_452.Solution2solution2;
13+
14+
@BeforeClass
15+
publicstaticvoidsetup() {
16+
solution1 =new_452.Solution1();
17+
solution2 =new_452.Solution2();
18+
}
19+
20+
@Test
21+
publicvoidtest1() {
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+
publicvoidtest2() {
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+
publicvoidtest3() {
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)

[8]ページ先頭

©2009-2025 Movatter.jp