题解 | #牛群的协作#
牛群的协作
https://www.nowcoder.com/practice/c065b35c5cff41429edbd6484096d708
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param cow_ranges int整型vector<vector<>>
* @return int整型
*/
int minParallelAttacks(vector<vector<int> >& cow_ranges) {
// write code here
int ans = 0;
sort(cow_ranges.begin(), cow_ranges.end(), [](const vector<int>& a, const vector<int> & b){
if (a[0] == b[0]) return a[1] < b[1];
return a[0] < b[0];
});
// 用来记录多头牛之间可以同时包含的最右的位置
int left = cow_ranges[0][1];
int start = 0;
for (int i = 1; i < cow_ranges.size(); i++) {
if (cow_ranges[i][0] > left) {
ans += 1;
left = cow_ranges[i][1];
start = i;
continue;
}
left = min(left, cow_ranges[i][1]);
}
ans += 1;
return ans;
}
};
阿里云工作强度 603人发布
查看6道真题和解析