Skip to content

Commit

Permalink
Solved q452 with C++ and Python3.
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinHaoranZhang committed Aug 11, 2021
1 parent b67fbee commit bd1e06a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
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;
}
};
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

0 comments on commit bd1e06a

Please sign in to comment.