mirror of
https://github.com/krahets/hello-algo.git
synced 2026-06-28 08:34:28 +00:00
954c45864b
* docs: add Japanese documents (`ja/docs`) * docs: add Japanese documents (`ja/codes`) * docs: add Japanese documents * Remove pythontutor blocks in ja/ * Add an empty at the end of each markdown file. * Add the missing figures (use the English version temporarily). * Add index.md for Japanese version. * Add index.html for Japanese version. * Add missing index.assets * Fix backtracking_algorithm.md for Japanese version. * Add avatar_eltociear.jpg. Fix image links on the Japanese landing page. * Add the Japanese banner. --------- Co-authored-by: krahets <krahets@163.com>
58 lines
1.9 KiB
Python
58 lines
1.9 KiB
Python
"""
|
|
File: coin_change_ii.py
|
|
Created Time: 2023-07-10
|
|
Author: krahets (krahets@163.com)
|
|
"""
|
|
|
|
|
|
def coin_change_ii_dp(coins: list[int], amt: int) -> int:
|
|
"""硬貨交換 II:動的プログラミング"""
|
|
n = len(coins)
|
|
# dp テーブルを初期化
|
|
dp = [[0] * (amt + 1) for _ in range(n + 1)]
|
|
# 最初の列を初期化
|
|
for i in range(n + 1):
|
|
dp[i][0] = 1
|
|
# 状態遷移
|
|
for i in range(1, n + 1):
|
|
for a in range(1, amt + 1):
|
|
if coins[i - 1] > a:
|
|
# 目標金額を超える場合、硬貨 i を選択しない
|
|
dp[i][a] = dp[i - 1][a]
|
|
else:
|
|
# 硬貨 i を選択しないのと選択するのとの両方の選択肢の和
|
|
dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]]
|
|
return dp[n][amt]
|
|
|
|
|
|
def coin_change_ii_dp_comp(coins: list[int], amt: int) -> int:
|
|
"""硬貨交換 II:空間最適化動的プログラミング"""
|
|
n = len(coins)
|
|
# dp テーブルを初期化
|
|
dp = [0] * (amt + 1)
|
|
dp[0] = 1
|
|
# 状態遷移
|
|
for i in range(1, n + 1):
|
|
# 順序で走査
|
|
for a in range(1, amt + 1):
|
|
if coins[i - 1] > a:
|
|
# 目標金額を超える場合、硬貨 i を選択しない
|
|
dp[a] = dp[a]
|
|
else:
|
|
# 硬貨 i を選択しないのと選択するのとの両方の選択肢の和
|
|
dp[a] = dp[a] + dp[a - coins[i - 1]]
|
|
return dp[amt]
|
|
|
|
|
|
"""ドライバーコード"""
|
|
if __name__ == "__main__":
|
|
coins = [1, 2, 5]
|
|
amt = 5
|
|
|
|
# 動的プログラミング
|
|
res = coin_change_ii_dp(coins, amt)
|
|
print(f"目標金額を構成する硬貨の組み合わせ数は {res}")
|
|
|
|
# 空間最適化動的プログラミング
|
|
res = coin_change_ii_dp_comp(coins, amt)
|
|
print(f"目標金額を構成する硬貨の組み合わせ数は {res}") |