10.10 题目总结(累计)
1.完成所有任务需要的最少轮数(思路)
思路:将数组的数依次放到map里面,如果有相同则在原有的基础上加一,然后全部放完之后,就遍历map,然后计算总次数,然后有一次的的则直接返回.
AC:
class Solution {public int minimumRounds(int[] tasks) {HashMap<Integer, Integer> taskCount = new HashMap<>();for (int task : tasks) {taskCount.put(task, taskCount.getOrDefault(task, 0) + 1);}int totalRounds = 0;// 计算最小的轮数for (int count : taskCount.values()) {if (count == 1) {return -1; // 如果某个任务只出现一次,无法完成}// 计算需要的轮数totalRounds += (count + 2) / 3; // 向上取整的简化方法}return totalRounds;}
}
java数组操作:
HashMao基础操作:
2.安排工作以为达到最大收益(简单的背包)
思路:先用背包思路求出每个难度下的最高利润,然后遍历工人能力相加就可以.难度升级:每个工作只能做一次
AC:
class Solution {public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) {int n = difficulty.length;int m = worker.length;// 创建一个数组来存储最大收益long[] maxProfitAtDifficulty = new long[100001]; // 假设工作难度不超过 100000// 先计算每个难度下的最大利润for (int i = 0; i < n; i++) {int diff = difficulty[i];int prof = profit[i];maxProfitAtDifficulty[diff] = Math.max(maxProfitAtDifficulty[diff], prof);}// 将最大收益进行累加,方便后续快速查询for (int i = 1; i < maxProfitAtDifficulty.length; i++) {maxProfitAtDifficulty[i] = Math.max(maxProfitAtDifficulty[i], maxProfitAtDifficulty[i - 1]);}int totalProfit = 0;// 遍历每个工人,计算他们的收益for (int w : worker) {totalProfit += maxProfitAtDifficulty[w]; // 根据工人的能力获取最大收益}return totalProfit; // 返回总收益}
}
3.