Skip to content

Commit 99936c3

Browse files
committed
Sync LeetCode submission Runtime - 703 ms (85.71%), Memory - 18.3 MB (32.24%)
1 parent 700f2be commit 99936c3

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

0322-coin-change/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p>
2+
3+
<p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p>
4+
5+
<p>You may assume that you have an infinite number of each kind of coin.</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
10+
<pre>
11+
<strong>Input:</strong> coins = [1,2,5], amount = 11
12+
<strong>Output:</strong> 3
13+
<strong>Explanation:</strong> 11 = 5 + 5 + 1
14+
</pre>
15+
16+
<p><strong class="example">Example 2:</strong></p>
17+
18+
<pre>
19+
<strong>Input:</strong> coins = [2], amount = 3
20+
<strong>Output:</strong> -1
21+
</pre>
22+
23+
<p><strong class="example">Example 3:</strong></p>
24+
25+
<pre>
26+
<strong>Input:</strong> coins = [1], amount = 0
27+
<strong>Output:</strong> 0
28+
</pre>
29+
30+
<p>&nbsp;</p>
31+
<p><strong>Constraints:</strong></p>
32+
33+
<ul>
34+
<li><code>1 &lt;= coins.length &lt;= 12</code></li>
35+
<li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li>
36+
<li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li>
37+
</ul>

0322-coin-change/solution.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Approach 3: (Dynamic programming - Bottom up)
2+
3+
# S = amount
4+
# Time: O(S * n)
5+
# Space: O(S)
6+
7+
class Solution:
8+
def coinChange(self, coins: List[int], amount: int) -> int:
9+
dp = [float('inf')] * (amount + 1)
10+
dp[0] = 0
11+
12+
for coin in coins:
13+
for x in range(coin, amount + 1):
14+
dp[x] = min(dp[x], dp[x - coin] + 1)
15+
16+
return dp[amount] if dp[amount] != float('inf') else -1
17+

0 commit comments

Comments
 (0)