每天刷一道牛客题霸-第24天- 换钱最少货币数

题目

https://www.nowcoder.com/practice/3911a20b3f8743058214ceaa099eeb45?tpId=190&&tqId=36067&rp=1&ru=/activity/oj&qru=/ta/job-code-high-rd/question-ranking

import java.util.*;


public class Solution {
    /**
     * 最少货币数
     * @param arr int整型一维数组 the array
     * @param aim int整型 the target
     * @return int整型
     */
   public static int minMoney (int[] arr, int aim) {
        int[] count = new int[aim +1];
        Arrays.fill(count, aim + 1);
        count[0] = 0;
        for (int i =1;i<=aim;i++){
            for (int j = 0 ; j < arr.length ; j++){
                if (i >=arr[j]){
                    count[i] = Math.min(count[i-arr[j]] +1 , count[i]);
                }
            }
        }
        return count[aim] != aim+1 ? count[aim] :-1;
        // write code here
    }
}
#牛客题霸##题解#
全部评论
点赞 回复 分享
发布于 2021-01-06 20:32

相关推荐

不愿透露姓名的神秘牛友
2025-12-18 11:21
优秀的大熊猫在okr...:叫你朋友入职保安,你再去送外卖,一个从商,一个从政,你们两联手无敌了,睁开你的眼睛看看,现在是谁说了算(校长在背后瑟瑟发抖)
选实习,你更看重哪方面?
点赞 评论 收藏
分享
许愿求offer:要有钩子,项目描述里必须有一两个让面试官忍不住想问的技术点
点赞 评论 收藏
分享
评论
2
1
分享

创作者周榜

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