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.最低票价(记忆化搜索(日期变量型),记忆化搜索(窗口变量型))

AC:

class Solution {
    int[] costs;
    Integer[] memo;
    Set<Integer> dayset;

    public int mincostTickets(int[] days, int[] costs) {
        this.costs = costs;
        memo = new Integer[366];
        dayset = new HashSet();
        for (int d: days) {
            dayset.add(d);
        }
        return dp(1);
    }

    public int dp(int i) {
        if (i > 365) {
            return 0;
        }
        if (memo[i] != null) {
            return memo[i];
        }
        if (dayset.contains(i)) {
            memo[i] = Math.min(Math.min(dp(i + 1) + costs[0], dp(i + 7) + costs[1]), dp(i + 30) + costs[2]);
        } else {
            memo[i] = dp(i + 1);
        }
        return memo[i];
    }
}

作者:力扣官方题解
链接:https://leetcode.cn/problems/minimum-cost-for-tickets/solutions/233810/zui-di-piao-jie-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

AC two:

class Solution {
    int[] days, costs;
    Integer[] memo;
    int[] durations = new int[]{1, 7, 30};

    public int mincostTickets(int[] days, int[] costs) {
        this.days = days;
        this.costs = costs;
        memo = new Integer[days.length];
        return dp(0);
    }

    public int dp(int i) {
        if (i >= days.length) {
            return 0;
        }
        if (memo[i] != null) {
            return memo[i];
        }
        memo[i] = Integer.MAX_VALUE;
        int j = i;
        for (int k = 0; k < 3; ++k) {
            while (j < days.length && days[j] < days[i] + durations[k]) {
                j++;
            }
            memo[i] = Math.min(memo[i], dp(j) + costs[k]);
        }
        return memo[i];
    }
}

作者:力扣官方题解
链接:https://leetcode.cn/problems/minimum-cost-for-tickets/solutions/233810/zui-di-piao-jie-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值