Skip to content

Commit

Permalink
coins problem DP & BackTracking solution
Browse files Browse the repository at this point in the history
  • Loading branch information
KPatr1ck committed Jan 1, 2019
1 parent 2a6ae2f commit 66f31db
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions python/41_dynamic_programming/coins_problem.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/python
# -*- coding: UTF-8 -*-

from typing import List


def coins_dp(values: List[int], target: int) -> int:
# memo[i]表示target为i的时候,所需的最少硬币数
memo = [0] * (target+1)
# 0元的时候为0个
memo[0] = 0

for i in range(1, target+1):
min_num = 999999
# 对于values中的所有n
# memo[i]为 min(memo[i-n1], memo[i-n2], ...) + 1
for n in values:
if i >= n:
min_num = min(min_num, 1 + memo[i-n])
else: # values中的数值要从小到大排序
break
memo[i] = min_num

# print(memo)
return memo[-1]


min_num = 999999
def coins_backtracking(values: List[int], target: int, cur_value: int, coins_count: int):
if cur_value == target:
global min_num
min_num = min(coins_count, min_num)
else:
for n in values:
if cur_value + n <= target:
coins_backtracking(values, target, cur_value+n, coins_count+1)


if __name__ == '__main__':
values = [1, 3, 5]
target = 23
print(coins_dp(values, target))
coins_backtracking(values, target, 0, 0)
print(min_num)

0 comments on commit 66f31db

Please sign in to comment.