forked from wangzheng0822/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request wangzheng0822#218 from KPatr1ck/dynamic_programming2
coins problem DP & BackTracking solution in Python
- Loading branch information
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|