8000 add 452 · louisfeng/Leetcode@c5bf94e · GitHub
[go: up one dir, main page]

Skip to content

Commit c5bf94e

Browse files
add 452
1 parent 5909e1b commit c5bf94e

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ Your ideas/fixes/algorithms are more than welcome!
9393
|455|[Assign Cookies](https://leetcode.com/problems/assign-cookies/)|[Solution](../master/src/main/java/com/stevesun/solutions/AssignCookies.java)| O(n)|O(1) | Easy|
9494
|454|[4Sum II](https://leetcode.com/problems/4sum-ii/)|[Solution](../master/src/main/java/com/stevesun/solutions/_454.java) | O(n) |O(n) | Medium| HashMap
9595
|453|[Minimum Moves to Equal Array Elements](https://leetcode.com/problems/minimum-moves-to-equal-array-elements/)|[Solution](../master/src/main/java/com/stevesun/solutions/MinimumMovestoEqualArrayElements.java)| O(n)|O(1) | Easy|
96+
|452|[Minimum Number of Arrows to Burst Balloons](https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/)|[Solution](../master/src/main/java/com/stevesun/solutions/_452.java) | O(nlogn) |O(1) | Medium| Array, Greedy
9697
|451|[Sort Characters By Frequency](https://leetcode.com/problems/sort-characters-by-frequency/)|[Solution](../master/src/main/java/com/stevesun/solutions/SortCharactersByFrequency.java) | O(nlogn) |O(n) | Medium| HashMap
9798
|449|[Serialize and Deserialize BST](https://leetcode.com/problems/serialize-and-deserialize-bst/)|[Solution](../master/src/main/java/com/stevesun/solutions/SerializeandDeserializeBST.java)| O(n)|O(h) | Medium| BFS
9899
|448|[Find All Numbers Disappeared in an Array](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/)|[Solution](../master/src/main/java/com/stevesun/solutions/FindAllNumbersDisappearedinanArray.java)| O(n)|O(1) | Easy| Array, HashMap

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.stevesun.solutions;
22

3+
import java.util.Arrays;
4+
35
/**
46
*452. Minimum Number of Arrows to Burst Balloons
57
*
@@ -27,8 +29,24 @@
2729
*/
2830
public class _452 {
2931

32+
//credit: https://discuss.leetcode.com/topic/66579/java-greedy-soution/6
3033
public int findMinArrowShots(int[][] points) {
31-
return 0;
34+
35+
if(points==null || points.length==0) return 0;
36+
// sort points based on their end point.
37+
Arrays.sort(points, (p1, p2) -> Integer.compare(p1[1],p2[1]));
38+
int currentEnd = points[0][1];
39+
int count = 1;
40+
for(int[] p: points)
41+
{
42+
// 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.
43+
if(p[0]>currentEnd) {
44+
count++;
45+
currentEnd = p[1];
46+
}
47+
else continue;
48+
}
49+
return count;
3250
}
3351

3452
}

0 commit comments

Comments
 (0)
0