首页 > 其他分享 >518_coins_changeII_找零钱II

518_coins_changeII_找零钱II

时间:2024-05-18 20:57:27浏览次数:13  
标签:changeII 零钱 int coins II 518 amount integer dp

问题描述

链接:https://leetcode.com/problems/coin-change-ii/

You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.'
Return the number of combinations that make up that amount. If that amount of money cannot be made up by any combination of the coins, return 0.
You may assume that you have an infinite number of each kind of coin.

The answer is guaranteed to fit into a signed 32-bit integer.

解释: 给定一些零钱\(coins\), 用一个整数amount表示金钱,将amount兑换成零钱,问总共有多少种方法?如果没有一种方法,则返回0

案例解析:

Input: amount = 5, coins = [1,2,5]
Output: 4
Explanation: there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
解释: amount = 5, 用数组中的零钱有4种兑换方案

基本思想

注意:零钱数量是无限的
这个是一个二维动态规划问题,假设coins的大小是n, 用m替换amount 则构建二维数组 dp, dp的大小为 n * (m+1)
\(dp[i][j]\) 表示 用coins[0~i-1]构成 amount=j的组合数目,则\(dp[i][j]\)更新公式如下:
\(dp[i][j]\) = \(dp[i-1][j]\) + \(dp[i][j-coins[i]]\)

  • \(dp[i-1][j]\) 表示不使用\(coins[i]\)构成amount=j 即不实用 coins[j]构成j的组合数目
  • \(dp[i][j-coin[i]]\) 表示 使用 \(coins[i]\) 构成 amount=j的数量

注意:这个问题和0-1背包问题比较像

动态规划

动态规划三种思路:

  • 一维
  • 一维不行上二维
  • 如果实在没有思路,就从简单入手,凭直觉。比如 股票买卖问题

代码

C++

    int change(int amount, vector<int>& coins) {
        int n = coins.size();
        if (n<=0) return 0;
        if (amount<=0) return 1;
        vector<vector<int>> dp(n, vector<int>(amount+1,0));
        sort(coins.begin(), coins.end()); // coins不能有重复

        for(int i=1;i<=amount;++i) {
            if (i%coins[0] == 0) {
                dp[0][i] = 1;
            }
        }
        for(int i=0;i<coins.size();++i) {
            dp[i][0] = 1; // 初始化存疑
        }
        for(int i=1;i<coins.size();++i) {
            for(int j=1;j<=amount;++j) {
                if (j<coins[i]) {
                    dp[i][j] = dp[i-1][j];
                } else {
                    dp[i][j] = dp[i-1][j] + dp[i][j-coins[i]];
                }
            }
        }
        return dp[n-1][amount];
    }

python

    def change(self, amount: int, coins: List[int]) -> int:
        n = len(coins)
        if amount<=0: return 1
        if n<=0: return 0
        dp = [[0 for j in range(0,amount+1)] for i in range(n)]
        for ele in range(0, amount+1):
            if ele % coins[0] == 0:
                dp[0][ele] = 1;
        for index in range(0, n): # amount=0的时候,初始化为1 
            dp[index][0] = 1
        for i in range(1,n):
            for j in range(1, amount+1):
                if j < coins[i]: 
                    dp[i][j] = dp[i-1][j]
                else:
                    dp[i][j] = dp[i-1][j] + dp[i][j-coins[i]]
        return dp[n-1][amount] 

标签:changeII,零钱,int,coins,II,518,amount,integer,dp
From: https://www.cnblogs.com/douniwanli/p/18199759

相关文章

  • Reflective Journal III
    1)Tomakeadigitalstory,youmustfirstwriteagoodscriptandstoryboard,sothatyouhaveaclearunderstandingandgraspofthedigitalstoryyouwanttotell.Posteriorly,IfoundanillustrationofthestoryfromtheInternetanduseditasaback......
  • 20240518模拟赛
    C240518A.传送门(portal)构造一个图使得点\(1\)到\(2\)的最短路正好有\(k\)条,使构造出的图点的个数\(N\len_5\)考虑\(k=2^t\)那么可以轻松构造出如下的图对于其他的情况可以考虑二进制拆分,如\(k=10\)时为了,使最短路长度固定加入点\(9\)对\(k=10^9\),只需构造\(80\)个点,可以......
  • Reflective Journal III
    ​Throughthisstudy,Ilearnedandmadeadigitalstory.First,chooseastory.Then,preparethenarrativescriptandrequiredpicturesorvideoclips.Afterarrangingallthepictures,adddubbing,subtitlesandbackgroundmusic.​Inmydigitalstory,I......
  • 非常全能WinForm 开发框架 - ReaLTaiizor
    欢迎ReaLTaiizor是一个用户友好的、以设计为中心的.NETWinForms项目控件库,包含广泛的组件。您可以使用不同的主题选项对项目进行个性化设置,并自定义用户控件,以使您的应用程序更加专业。项目地址:https://github.com/Taiizor/ReaLTaiizor步骤1:添加ReaLTaiizor的引用或在NuGet上搜......
  • 122- Best Time to Buy and Sell Stock II 卖股票II
    题目描述链接:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/Youaregivenanintegerarraypriceswhereprices[i]isthepriceofagivenstockontheithday.Oneachday,youmaydecidetobuyand/orsellthestock.Youcanon......
  • 有效回文 II
    题目链接:来自罗勇军《算法竞赛》尺取法一节的习题。思路:反向扫描,设双指针为\(i\)和\(j\)。if(s[i]==s[j])i++,j--;否则的话要么删除\(s[i]\)或者删除\(s[j]\),看剩下的字符串是否是回文串。classSolution{public:boolcheck(stringstr,intl,intr){......
  • 51模拟IIC-页读写操作
    51代码页读写IIC--模拟IIC#include<reg52.h>#include<intrins.h>sbitSDA=P0^0;sbitSCL=P0^1;sbitLED=P2^0;unsignedcharcodetable[]={0x1c,0X3B,0X2C,0X2D,0X5A,0X5C,0XC5,0X5b};voiddelayms(unsignedintt){unsignedinti,j;fo......
  • 40. 组合总和 II(leetcode)
    https://leetcode.cn/problems/combination-sum-ii/description/classSolution{List<List<Integer>>res=newArrayList<>();LinkedList<Integer>path=newLinkedList<>();intsum=0;publicList<List&l......
  • 抽象代数课程笔记 III —— 域论、伽罗瓦理论
    持续更新。\(\newcommand{\a}{\alpha}\newcommand{\b}{\beta}\newcommand{\D}{\Delta}\newcommand{\eps}{\varepsilon}\newcommand{\ph}{\varphi}\newcommand{\t}{\theta}\newcommand{\la}{\lambda}\newcommand{\si}{\sigma}\newcommand{\d}{......
  • 抽象代数课程笔记 III —— 域论、伽罗瓦理论
    持续更新。\(\newcommand{\a}{\alpha}\newcommand{\b}{\beta}\newcommand{\D}{\Delta}\newcommand{\eps}{\varepsilon}\newcommand{\ph}{\varphi}\newcommand{\t}{\theta}\newcommand{\la}{\lambda}\newcommand{\si}{\sigma}\newcommand{\d}{......