【Leetcode】322. Coin Change

You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.

Example 1:

coins = [1, 2, 5], amount = 11

return 3 (11 = 5 + 5 + 1)

Example 2:

coins = [2], amount = 3

return -1.

Tips:给定一个coins[]数组,表示现有的硬币类型;给定一个整数amount表示要组成的金钱总数。

根据coins[],求出组成amount所需的最少的硬币数。

解法一:循环 :①初始化一个数组dp,并将数组中的值都赋值为Integer.MAX_VALUE;

② 两层循环,第一层遍历amount 第二层遍历coins[].length,当总钱数减去一个硬币的值小于0,证明无法组成该钱数,continue。或者上一步的dp值仍为初始值Integer.MAX_VALUE,也应continue;

都满足条件时,dp选择当前dp值与上一步dp值加一后的最小值,dp[i]=Math.min(dp[i],1+dp[i-coins[j]] );。

③如果无法组成amount这个钱数,就返回-1,否则返回dp[amount].

public int coinChange(int[] coins, int amount) {
        int[] dp= new int[amount+1];
        for(int i=0;i<=amount;i++){
            dp[i]=Integer.MAX_VALUE;
        }
        dp[0]=0;
        for(int i=1;i<=amount;i++){
            for(int j=0;j<coins.length;j++){
                if(i-coins[j]<0 ||dp[i-coins[j]]==Integer.MAX_VALUE) continue;
                dp[i]=Math.min(dp[i],1+dp[i-coins[j]] );
            }
        }
        return dp[amount]==Integer.MAX_VALUE?-1:dp[amount];
    }

解法二: 递归 :当前组成amount所需的硬币数,与上一步有关.假设我们已经找到了能组成amount的最少硬币数,那么最后一步,我们可以选择任意的一个硬币,加入这个硬币之前,组成的金钱数为r,这时,r = amount-coins[i](需要循环所有的coins)。依次向前推,直到r等于0或者小于0.

public int coinChange(int[] coins, int amount) {        if (amount < 0)            return 0;        return coinChangeCore(coins, amount, new int[amount]);    }

    private int coinChangeCore(int[] coins, int amount, int[] count) {        if (amount < 0)            return -1;        if (amount == 0)            return 0;        //剪枝        if (count[amount - 1] != 0)            return count[amount - 1];        int min = Integer.MAX_VALUE;        for (int i = 0; i < coins.length; i++) {            int ans = coinChangeCore(coins, amount - coins[i], count);            if (ans >= 0 && ans < min) {                min = 1 + ans;            }        }        count[amount - 1] = (min == Integer.MAX_VALUE) ? -1 : min;        return count[amount - 1];    }

原文地址:https://www.cnblogs.com/yumiaomiao/p/8445816.html

时间: 2024-09-27 16:19:26

【Leetcode】322. Coin Change的相关文章

LeetCode OJ 322. Coin Change DP求解

题目链接:https://leetcode.com/problems/coin-change/ 322. Coin Change My Submissions Question Total Accepted: 15289 Total Submissions: 62250 Difficulty: Medium You are given coins of different denominations and a total amount of money amount. Write a func

Leetcode OJ --- 322. Coin Change

You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins,

Leetcode solution 322: Coin Change

Problem Statement  You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combin

【leetcode】Generate Parentheses

题目: 给定整数n,返回n对匹配的小括号字符串数组. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" 分析: 这种问题的模式是:1)问题的解有多个 ,2)每个解都是由多个有效的 "步骤" 组成的,3)变更以有解的某个或某些"步骤"

【LeetCode】Implement strStr()

Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack. 标准KMP算法.可参考下文. http://blog.csdn.net/yaochunnian/article/details/7059486 核心思想在于求出模式串前缀与后缀中重复部分,将重复信息保存在n

【LeetCode】Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 ->

【LeetCode】Pascal&#39;s Triangle

Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 这题别想用通项公式做,n choose m里面的连乘必然溢出,老老实实逐层用定义做. class Solution { public: vector<vector<

【LeetCode】Copy List with Random Pointer

A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list. 思路:第一遍正常复制链表,同时用哈希表保存链表中原始节点和新节点的对应关系,第二遍遍历链表的时候,再复制随机域. 这是一种典型的空间换时间的做法,n个节点,需要大小为O(n

【leetcode】Max Points on a Line (python)

给定一个点,除该点之外的其他所有点中,与该点的关系要么是共线,要么就是共点,也就是两点重合. 共线有三种情况:水平共线,垂直共线,倾斜的共线.合并下这三种情况就是斜率存在的共线和斜率不存在的共线. 那么我们的任务就是针对每个点,找出与其共线的这些情况中,共线最多的点的个数. 注意:最终的结果别忘了加上共点的个数. class Solution: def maxPoints(self, points ): if len( points ) <= 1: return len( points ) ma