C++ simple solution


  • 0
    N
    bool isSmaller(pair<int, int>& a, pair<int, int>& b)
    {
        if (a.first < b.first)
            return true;
        if (a.first == b.first)
            if (a.second <= b.second)
                return true;
        return false;
    }
    
    class Solution {
    public:
        bool isOverlapped(pair<int, int>& a, pair<int, int>& b)
        {
            if(b.first <= a.second)
            {
                a.first = max(a.first, b.first);
                a.second = min(a.second, b.second);
                return true;
            }
            return false;
        }
        
        int findMinArrowShots(vector<pair<int, int>>& points) 
        {
            int counts = 0, removed = 0, idx = 0;
            sort(points.begin(), points.end(), isSmaller);
            
            while(removed < points.size())
            {
                int startIdx = idx;
                while(idx < points.size() && isOverlapped(points[startIdx], points[++idx]))
                {
                    removed++;
                }
                removed++;
                counts++;
            }
            
            return counts;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.