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]
相关推荐
模拟器连接器曾工10 分钟前
AI视觉检测设备参数有哪些?从硬件到算法的全面解析
人工智能·算法·视觉检测·ai视觉·ai视觉检测
量子物理学11 分钟前
Open CV 边缘检测算法:Canny、Sobel、Scharr与Laplacian对比解析
人工智能·算法·计算机视觉
.柒宇.13 分钟前
力扣hot 100之和为 K 的子数组(Java版)
java·算法·leetcode
Byte不洛16 分钟前
LeetCode中经典双指针题(环形链表 + 快乐数 + 移动零)
算法·leetcode·链表·数组·双指针
Boop_wu17 分钟前
[Java 算法] 快速排序和快速选择排序(※)
数据结构·算法·排序算法
人间打气筒(Ada)19 分钟前
「码动四季·开源同行」golang:负载均衡如何提高系统可用性?
算法·golang·开源·go·负载均衡·负载均衡算法
司马万21 分钟前
RUST基础1----数据类型
开发语言·算法·rust
卷福同学25 分钟前
Claude Code源码泄露:8大隐藏功能曝光
人工智能·后端·算法
阿豪学编程10 小时前
LeetCode724.:寻找数组的中心下标
算法·leetcode
墨韵流芳10 小时前
CCF-CSP第41次认证第三题——进程通信
c++·人工智能·算法·机器学习·csp·ccf