换钱最少的货币数

换钱的最少货币数

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

   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]){
                    //当前的钱数-当前面值,为之前换过的钱数,如果能够兑换只需要在加+1即可,如果不能就取aim+1;
                    count[i] = Math.min(count[i-arr[j]] +1 , count[i]);
                }
            }
        }
        //对应的总数是否能够兑换取决于是否等于aim+1
        return count[aim] != aim+1 ? count[aim] :-1;
        // write code here
    }
全部评论

相关推荐

喜欢飞来飞去的雪碧在刷代码:可以试一试字节
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

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