-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path322.py
More file actions
25 lines (21 loc) · 699 Bytes
/
322.py
File metadata and controls
25 lines (21 loc) · 699 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
from typing import List
# Name: Coin Change
# Link: https://leetcode.com/problems/coin-change/
# Method: DP, build to the target amount
# Time: O(m \* n)
# Space: O(m)
# Difficulty: Medium
# Notes: m = amount, n = nr of coins
class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
way_to_reach = [-1] * (amount + 1)
way_to_reach[0] = 0
for i in range(1, amount + 1):
ways = [
way_to_reach[i - coin]
for coin in coins
if i - coin >= 0 and way_to_reach[i - coin] != -1
]
if ways:
way_to_reach[i] = min(ways) + 1
return way_to_reach[amount]