题解 | #换钱的最少货币数#| JAVA| DFS | BFS | 动态规划 |

换钱的最少货币数

http://www.nowcoder.com/practice/3911a20b3f8743058214ceaa099eeb45

懒。

动态规划

  public int minMoney (int[] arr, int aim) {
// 初始化
        table = new int[aim+1];
        return dp(arr,aim);
    }
    // 进行剪枝 , 一般动态规划都要
    int[] table ;
     /**
     * 定义 , 要凑出金额 amount , 至少需要  dp(amount) 个硬币
     *
     * @param arr
     * @param amount 要凑的金额
     * @return 硬币数量
     */
    public int dp(int[] arr, int amount){
        //base case  基本返回操作, 当金额等于的0 时候就不用返回了。
        if(amount == 0){
            return 0;
        }
        if(amount < 0){
            return -1;
        }
        // 返回某个金额的最少 金币数
        if(table[amount]!=0){
            return table[amount];
        }

        int res = Integer.MAX_VALUE;

        for (int i = 0; i < arr.length; i++) {
             //遍历金币数组,每个可能都尝试
            int subproblem = dp(arr, amount - arr[i]);
            // 无解跳过
            if(subproblem == -1){
                continue;
            }
            res = Math.min(subproblem + 1, res);
        }
        res = res ==  Integer.MAX_VALUE ? -1 : res;
        table[amount] = res;
        return res;
    }

DFS

   public int minMoney(int[] arr, int amount) {
        Arrays.sort(arr);
        dfs(arr, arr.length - 1, 0, amount);
        return res == Integer.MAX_VALUE ? -1 : res;
    }

    int res = Integer.MAX_VALUE;

    public void dfs(int[] coins, int index, long count, int amount) {
        //剪枝,  下标小于0 , 或者 比 res 大的没有必要统计
        if (index < 0 || count + amount / coins[index] >= res)
            return;
        // 计算
        if (amount % coins[index] == 0) {
            res = Math.min(res, (int) count + amount / coins[index]);
            return;
        }
        // 递归
        for (int i = amount / coins[index]; i >= 0; i--) {
            dfs(coins, index - 1, count + i, amount - i * coins[index]);
        }
    }

BFS

   public int minMoney(int[] coins, int amount) {
        int count = 0;
        Deque<Integer> deque = new ArrayDeque<>();
        deque.add(amount);
        int[] table = new int[amount + 1];

        while (!deque.isEmpty()) {
            int len = deque.size();
            for (int i = 0; i < len; i++) {
                int temp = deque.pop();
                if (temp == 0) {
                    return count;
                }
                for (int coin : coins) {
                    if (temp >= coin && table[temp - coin] == 0) {
                        deque.add(temp - coin);
                        table[temp - coin] = 1;
                    }
                }
            }
            count += 1;
        }
        return -1;
    }
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-11 11:24
大家还是用ai改吧,我心疼得要死,就当花钱买教训吧,人家直接拿完钱就跑路了
程序员小白条:简历修改700....神奇,又不是帮你面试,咋的,简历修改从双非变92了还是没实习变成有大厂实习了
点赞 评论 收藏
分享
湫湫湫不会java:先投着吧,大概率找不到实习,没实习的时候再加个项目,然后把个人评价和荣誉奖项删了,赶紧成为八股战神吧,没实习没学历,秋招机会估计不多,把握机会。或者说秋招时间去冲实习,春招冲offer,但是压力会比较大
点赞 评论 收藏
分享
苍蓝星上艾露:这简历。。。可以试试我写的开源简历优化工具https://github.com/weicanie/prisma-ai
点赞 评论 收藏
分享
今天 12:15
门头沟学院 Java
点赞 评论 收藏
分享
评论
2
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务