Leetcode 518. Coin Change II

Problem

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.

Algorithm

Dynamic Programming (DP). Complete knapsack problem, forward to sum the ways.

Code

python3 复制代码
class Solution:
    def change(self, amount: int, coins: List[int]) -> int:
        dp = [0] * (amount + 1)
        dp[0] = 1
        for coin in coins:
            for v in range(coin, amount+1):
                dp[v] += dp[v - coin]
        
        return dp[amount]
相关推荐
Elias不吃糖18 小时前
LeetCode每日一练(209, 167)
数据结构·c++·算法·leetcode
铁手飞鹰19 小时前
单链表(C语言,手撕)
数据结构·c++·算法·c·单链表
悦悦子a啊19 小时前
项目案例作业(选做):使用文件改造已有信息系统
java·开发语言·算法
小殊小殊19 小时前
【论文笔记】知识蒸馏的全面综述
人工智能·算法·机器学习
无限进步_19 小时前
C语言动态内存管理:掌握malloc、calloc、realloc和free的实战应用
c语言·开发语言·c++·git·算法·github·visual studio
im_AMBER19 小时前
AI井字棋项目开发笔记
前端·笔记·学习·算法
Wadli19 小时前
项目2 |内存池1|基于哈希桶的多种定长内存池
算法
TT哇19 小时前
【BFS 解决拓扑排序】3. ⽕星词典(hard)
redis·算法·宽度优先
橘颂TA19 小时前
【剑斩OFFER】算法的暴力美学——判定字符是否唯一
算法·c/c++·结构与算法
ModestCoder_20 小时前
PPO-clip算法在Gymnasium的Pendulum环境实现
人工智能·算法·机器人·具身智能