-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b67fbee
commit bd1e06a
Showing
2 changed files
with
38 additions
and
0 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
Algorithm/Greedy/452_minimum-number-of-arrows-to-burst-balloons/q452.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
class Solution { | ||
public: | ||
int findMinArrowShots(vector<vector<int>>& points) { | ||
if (points.empty()) { | ||
return 0; | ||
} | ||
// sort by end points | ||
sort(points.begin(), points.end(), [](vector<int>& a, vector<int>& b)->bool{ | ||
if(a[1] == b[1]){ | ||
return a[0] < b[0]; | ||
} | ||
|
||
return a[1] < b[1]; | ||
}); | ||
int arrow = 1, prev_end = points[0][1]; | ||
for (int i = 1; i < points.size(); ++i) { | ||
// within range, only one arrow needed | ||
if (points[i][0] <= prev_end) { | ||
continue; | ||
} | ||
++arrow; | ||
prev_end = points[i][1]; | ||
} | ||
return arrow; | ||
} | ||
}; |
12 changes: 12 additions & 0 deletions
12
Algorithm/Greedy/452_minimum-number-of-arrows-to-burst-balloons/q452.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class Solution: | ||
def findMinArrowShots(self, points: List[List[int]]) -> int: | ||
if len(points) == 0: | ||
return 0 | ||
points.sort(key = lambda x:x[1]) | ||
prev_end, arrow = points[0][1], 1 | ||
for i in range(1, len(points)): | ||
if points[i][0] <= prev_end: | ||
continue | ||
arrow += 1 | ||
prev_end = points[i][1] | ||
return arrow |