Skip to content

Commit 9f23fbf

Browse files
committed
Create 2025-05-10-Leetcode-322.md
1 parent d7ba36d commit 9f23fbf

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
title: Leetcode 322. Coin Change
3+
description: Explanation for Dynamic Programming, Medium, and its solution in Python.
4+
date: 2025-05-10
5+
categories: [Leetcode, Dynamic Programming, Medium]
6+
tags: [Leetcode, Python, Study, Dynamic Programming, Medium]
7+
math: true
8+
---
9+
10+
## Problem
11+
[ Dynamic Programming, Medium](https://leetcode.com/problems/coin-change/description/)
12+
13+
Example:
14+
```
15+
Input: coins = [1,2,5], amount = 11
16+
Output: 3
17+
Explanation: 11 = 5 + 5 + 1
18+
19+
Input: coins = [2], amount = 3
20+
Output: -1
21+
22+
Input: coins = [1], amount = 0
23+
Output: 0
24+
```
25+
26+
## Approach
27+
28+
Using dynamic programming, we can solve for the counts by storing all the coin values in dp array
29+
30+
Here is the Python code for the solution:
31+
```python
32+
class Solution:
33+
def coinChange(self, coins: List[int], amount: int) -> int:
34+
dp = [float('inf')] * (amount+1)
35+
dp[0] = 0
36+
37+
for a in range(amount):
38+
for c in coins:
39+
if a-c >= 0:
40+
dp[a] = min(dp[a], 1+dp[a-c])
41+
42+
return dp[amonut] if dp[amount] != float('inf') else return -1
43+
44+
```
45+
## Time Complexity and Space Complexity
46+
47+
Time Complexity: $O(n * t)$ where $n$ is length of coins $t$ is amount
48+
49+
Space Complexity: $O(t)$

0 commit comments

Comments
 (0)