题解 | #牛群的协作#
牛群的协作
https://www.nowcoder.com/practice/c065b35c5cff41429edbd6484096d708
题目考察的知识点
考察贪心算法
题目解答方法的文字分析
首先将二维数组按照右边界升序排序之后进行遍历。当发现左侧边界大于右侧边界的时候说明遇到一个新的数组,需要一次新的攻击,将攻击的次数计数返回即可。
本题解析所用的编程语言
使用Java解答
完整且正确的编程代码
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param cow_ranges int整型二维数组
* @return int整型
*/
public int minParallelAttacks (int[][] cow_ranges) {
// write code here
int n = cow_ranges.length;
Arrays.sort(cow_ranges, Comparator.comparingInt(o -> o[1]));
int count = 0, temp = Integer.MIN_VALUE;
for(int[] cows : cow_ranges){
if(cows[0] > temp){
count++;
temp = cows[1];
}
}
return count;
}
}
顺丰集团工作强度 335人发布
查看14道真题和解析