GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode/python/322_Coin_Change.py at master · qiyuangong/leetcode

Expand file tree

/

322_Coin_Change.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
48 lines (46 loc) · 1.48 KB
/

322_Coin_Change.py

Copy path

File metadata and controls

48 lines (46 loc) · 1.48 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
43
44
45
46
47
48
class Solution(object):
# def coinChange(self, coins, amount):
# """
# :type coins: List[int]
# :type amount: int
# :rtype: int
# """
# # Top-down dp
# if amount < 1:
# return 0
# return self.coinChange_helper(coins, amount, [0] * (amount + 1))
# def coinChange_helper(self, coins, amount, count):
# if amount < 0:
# return -1
# if amount == 0:
# return 0
# if count[amount - 1] != 0:
# return count[amount - 1]
# min_count = 1000000000
# for coin in coins:
# res = self.coinChange_helper(coins, amount - coin, count)
# if res >= 0 and res < min_count:
# min_count = 1 + res
# if min_count == 1000000000:
# count[amount - 1] = -1
# else:
# count[amount - 1] = min_count
# return count[amount - 1]
def coinChange(self, coins, amount):
# bottom-up dp
if amount == 0:
return 0
if coins is None or len(coins) == 0:
return -1
coins.sort()
dp = [1000000000] * (amount + 1)
for i in range(1, amount + 1):
for coin in coins:
if i == coin:
dp[i] = 1
break
elif i > coin:
dp[i] = min(dp[i], dp[i - coin] + 1)
if dp[amount] == 1000000000:
return -1
return dp[amount]
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.