题解 | #牛群的协作#

牛群的协作

https://www.nowcoder.com/practice/c065b35c5cff41429edbd6484096d708

#include <vector>
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param cow_ranges int整型vector<vector<>> 
     * @return int整型
     */
    int minParallelAttacks(vector<vector<int> >& cow_ranges) {
        // write code here
        int length = cow_ranges.size();
        sort(cow_ranges.begin(), cow_ranges.end(), [](const vector<int> &a, const vector<int> &b){
            return a[1] < b[1];
        });
        int place = cow_ranges[0][1];
        int res = 1;
        for(int i=1 ; i<length ; ++i){
            if(cow_ranges[i][0] > place){
                ++res;
                place = cow_ranges[i][1];
            }
        }
        return res;
    }
};

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务