Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2e427ed

Browse files
committedMar 4, 2025
Add solution #518
1 parent b7628c1 commit 2e427ed

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed
 

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@
414414
515|[Find Largest Value in Each Tree Row](./0515-find-largest-value-in-each-tree-row.js)|Medium|
415415
516|[Longest Palindromic Subsequence](./0516-longest-palindromic-subsequence.js)|Medium|
416416
517|[Super Washing Machines](./0517-super-washing-machines.js)|Hard|
417+
518|[Coin Change II](./0518-coin-change-ii.js)|Medium|
417418
520|[Detect Capital](./0520-detect-capital.js)|Easy|
418419
521|[Longest Uncommon Subsequence I](./0521-longest-uncommon-subsequence-i.js)|Easy|
419420
530|[Minimum Absolute Difference in BST](./0530-minimum-absolute-difference-in-bst.js)|Easy|

‎solutions/0518-coin-change-ii.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* 518. Coin Change II
3+
* https://leetcode.com/problems/coin-change-ii/
4+
* Difficulty: Medium
5+
*
6+
* You are given an integer array coins representing coins of different denominations and
7+
* an integer amount representing a total amount of money.
8+
*
9+
* Return the number of combinations that make up that amount. If that amount of money
10+
* cannot be made up by any combination of the coins, return 0.
11+
*
12+
* You may assume that you have an infinite number of each kind of coin.
13+
*
14+
* The answer is guaranteed to fit into a signed 32-bit integer.
15+
*/
16+
17+
/**
18+
* @param {number} amount
19+
* @param {number[]} coins
20+
* @return {number}
21+
*/
22+
var change = function(amount, coins) {
23+
const dp = new Array(amount + 1).fill(0);
24+
dp[0] = 1;
25+
26+
for (const coin of coins) {
27+
for (let i = coin; i <= amount; i++) {
28+
dp[i] += dp[i - coin];
29+
}
30+
}
31+
32+
return dp[amount];
33+
};

0 commit comments

Comments
 (0)
Please sign in to comment.