Accepted concise c++ solution with comment


  • 0
    class Solution {
    public:
        int findMinArrowShots(vector<pair<int, int>>& points) {
            if(!points.size()) return 0;
            sort(points.begin(), points.end()); // Default comparison function can handle pairs, not necessary to customize sorting function. 
    
            int result = 1, end_index = points[0].second;
            for(int i = 1; i < points.size(); i++){
                if(points[i].first <= end_index){  
    		// For cases like [[1,4], [2,3]], update end_index, do not increase result;
                    end_index = min(end_index, points[i].second);
                }else{
    		// If two pairs don't overlap, we will need one more shot 
                    result++;
                    end_index = points[i].second;
                }
            }
            return result;
        }
    };
    

Log in to reply
 

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