-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathleetcode_518.py
More file actions
42 lines (30 loc) · 992 Bytes
/
leetcode_518.py
File metadata and controls
42 lines (30 loc) · 992 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 22 13:21:36 2026
@author: rishigoswamy
Problem:
#https://leetcode.com/problems/coin-change-ii/description/
Count number of combinations to make 'amount'
using unlimited supply of given coins.
Approach:
2D Dynamic Programming
dp[i][j] =
number of ways to make amount j
using first i coins
Time Complexity: O(n * amount)
Space Complexity: O(n * amount)
"""
from typing import List
class Solution:
def change(self, amount: int, coins: List[int]) -> int:
dp = [[0] * (amount + 1) for _ in range(len(coins)+1)]
for i in range(0, len(dp)):
dp[i][0] = 1
for i in range(1,len(dp)):
for j in range(len(dp[0])):
currCoin = coins[i-1]
dp[i][j] = dp[i-1][j]
if j-currCoin >= 0:
dp[i][j] += dp[i][j-currCoin]
return dp[-1][-1]