Skip to content

Instantly share code, notes, and snippets.

@ganesh-k13
Created October 10, 2020 16:30
Show Gist options
  • Save ganesh-k13/526a7e81a20e100474b61243015305a7 to your computer and use it in GitHub Desktop.
Save ganesh-k13/526a7e81a20e100474b61243015305a7 to your computer and use it in GitHub Desktop.
Minimum Number of Arrows to Burst Balloons Solution
class Solution:
def findMinArrowShots(self, points: List[List[int]]) -> int:
if points == []:
return 0
points.sort(key = lambda p: p[1])
res = 1
cur_top = points[0][1]
for point in points:
if point[0] <= cur_top:
continue
cur_top = point[1]
res+=1
return res
@channabankapur
Copy link

channabankapur commented Oct 10, 2020

bool compByEnd(const vector<int>& lhs, const vector<int>& rhs) {

  if(lhs[1] < rhs[1]) return true;
  return false;
}

class Solution {
public:
    int findMinArrowShots(vector<vector<int>>& points) {
        if(points.size() == 0) return 0;
        sort(points.begin(), points.end(), compByEnd);
        int lastEnd = points[0][1];
        int arrows = 1;
        for(auto& point: points) {
            if(point[0] > lastEnd) {
                arrows++;
                lastEnd = point[1];
            }
        }
        return arrows;
    }
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment