mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-01 11:29:51 +08:00
docs: add Japanese translate documents (#1812)
* 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>
This commit is contained in:
committed by
GitHub
parent
2487a27036
commit
954c45864b
48
ja/codes/python/chapter_greedy/coin_change_greedy.py
Normal file
48
ja/codes/python/chapter_greedy/coin_change_greedy.py
Normal file
@ -0,0 +1,48 @@
|
||||
"""
|
||||
File: coin_change_greedy.py
|
||||
Created Time: 2023-07-18
|
||||
Author: krahets (krahets@163.com)
|
||||
"""
|
||||
|
||||
|
||||
def coin_change_greedy(coins: list[int], amt: int) -> int:
|
||||
"""硬貨交換:貪欲法"""
|
||||
# coins リストがソートされていると仮定
|
||||
i = len(coins) - 1
|
||||
count = 0
|
||||
# 残り金額がなくなるまで貪欲選択をループ
|
||||
while amt > 0:
|
||||
# 残り金額に最も近く、それより小さい硬貨を見つける
|
||||
while i > 0 and coins[i] > amt:
|
||||
i -= 1
|
||||
# coins[i] を選択
|
||||
amt -= coins[i]
|
||||
count += 1
|
||||
# 実行可能な解が見つからない場合、-1 を返す
|
||||
return count if amt == 0 else -1
|
||||
|
||||
|
||||
"""ドライバーコード"""
|
||||
if __name__ == "__main__":
|
||||
# 貪欲法:大域最適解の発見を保証できる
|
||||
coins = [1, 5, 10, 20, 50, 100]
|
||||
amt = 186
|
||||
res = coin_change_greedy(coins, amt)
|
||||
print(f"\ncoins = {coins}, amt = {amt}")
|
||||
print(f"{amt} を構成するのに必要な硬貨の最小数は {res}")
|
||||
|
||||
# 貪欲法:大域最適解の発見を保証できない
|
||||
coins = [1, 20, 50]
|
||||
amt = 60
|
||||
res = coin_change_greedy(coins, amt)
|
||||
print(f"\ncoins = {coins}, amt = {amt}")
|
||||
print(f"{amt} を構成するのに必要な硬貨の最小数は {res}")
|
||||
print(f"実際には必要な最小数は 3、つまり 20 + 20 + 20")
|
||||
|
||||
# 貪欲法:大域最適解の発見を保証できない
|
||||
coins = [1, 49, 50]
|
||||
amt = 98
|
||||
res = coin_change_greedy(coins, amt)
|
||||
print(f"\ncoins = {coins}, amt = {amt}")
|
||||
print(f"{amt} を構成するのに必要な硬貨の最小数は {res}")
|
||||
print(f"実際には必要な最小数は 2、つまり 49 + 49")
|
||||
46
ja/codes/python/chapter_greedy/fractional_knapsack.py
Normal file
46
ja/codes/python/chapter_greedy/fractional_knapsack.py
Normal file
@ -0,0 +1,46 @@
|
||||
"""
|
||||
File: fractional_knapsack.py
|
||||
Created Time: 2023-07-19
|
||||
Author: krahets (krahets@163.com)
|
||||
"""
|
||||
|
||||
|
||||
class Item:
|
||||
"""アイテム"""
|
||||
|
||||
def __init__(self, w: int, v: int):
|
||||
self.w = w # アイテムの重量
|
||||
self.v = v # アイテムの価値
|
||||
|
||||
|
||||
def fractional_knapsack(wgt: list[int], val: list[int], cap: int) -> int:
|
||||
"""分数ナップサック:貪欲法"""
|
||||
# アイテムリストを作成、2 つの属性を含む:重量、価値
|
||||
items = [Item(w, v) for w, v in zip(wgt, val)]
|
||||
# 単位価値 item.v / item.w で高い順にソート
|
||||
items.sort(key=lambda item: item.v / item.w, reverse=True)
|
||||
# 貪欲選択をループ
|
||||
res = 0
|
||||
for item in items:
|
||||
if item.w <= cap:
|
||||
# 残り容量が十分な場合、アイテム全体をナップサックに入れる
|
||||
res += item.v
|
||||
cap -= item.w
|
||||
else:
|
||||
# 残り容量が不十分な場合、アイテムの一部をナップサックに入れる
|
||||
res += (item.v / item.w) * cap
|
||||
# 残り容量がなくなったため、ループを中断
|
||||
break
|
||||
return res
|
||||
|
||||
|
||||
"""ドライバーコード"""
|
||||
if __name__ == "__main__":
|
||||
wgt = [10, 20, 30, 40, 50]
|
||||
val = [50, 120, 150, 210, 240]
|
||||
cap = 50
|
||||
n = len(wgt)
|
||||
|
||||
# 貪欲アルゴリズム
|
||||
res = fractional_knapsack(wgt, val, cap)
|
||||
print(f"ナップサック容量を超えないアイテムの最大値は {res}")
|
||||
33
ja/codes/python/chapter_greedy/max_capacity.py
Normal file
33
ja/codes/python/chapter_greedy/max_capacity.py
Normal file
@ -0,0 +1,33 @@
|
||||
"""
|
||||
File: max_capacity.py
|
||||
Created Time: 2023-07-21
|
||||
Author: krahets (krahets@163.com)
|
||||
"""
|
||||
|
||||
|
||||
def max_capacity(ht: list[int]) -> int:
|
||||
"""最大容量:貪欲法"""
|
||||
# i、j を初期化、配列の両端で分割させる
|
||||
i, j = 0, len(ht) - 1
|
||||
# 初期最大容量は 0
|
||||
res = 0
|
||||
# 2 つの板が出会うまで貪欲選択をループ
|
||||
while i < j:
|
||||
# 最大容量を更新
|
||||
cap = min(ht[i], ht[j]) * (j - i)
|
||||
res = max(res, cap)
|
||||
# 短い板を内側に移動
|
||||
if ht[i] < ht[j]:
|
||||
i += 1
|
||||
else:
|
||||
j -= 1
|
||||
return res
|
||||
|
||||
|
||||
"""ドライバーコード"""
|
||||
if __name__ == "__main__":
|
||||
ht = [3, 8, 5, 2, 7, 7, 3, 4]
|
||||
|
||||
# 貪欲アルゴリズム
|
||||
res = max_capacity(ht)
|
||||
print(f"最大容量は {res}")
|
||||
33
ja/codes/python/chapter_greedy/max_product_cutting.py
Normal file
33
ja/codes/python/chapter_greedy/max_product_cutting.py
Normal file
@ -0,0 +1,33 @@
|
||||
"""
|
||||
File: max_product_cutting.py
|
||||
Created Time: 2023-07-21
|
||||
Author: krahets (krahets@163.com)
|
||||
"""
|
||||
|
||||
import math
|
||||
|
||||
|
||||
def max_product_cutting(n: int) -> int:
|
||||
"""切断の最大積:貪欲法"""
|
||||
# n <= 3 の場合、1 を切り出す必要がある
|
||||
if n <= 3:
|
||||
return 1 * (n - 1)
|
||||
# 貪欲的に 3 を切り出す、a は 3 の個数、b は余り
|
||||
a, b = n // 3, n % 3
|
||||
if b == 1:
|
||||
# 余りが 1 の場合、1 * 3 のペアを 2 * 2 に変換
|
||||
return int(math.pow(3, a - 1)) * 2 * 2
|
||||
if b == 2:
|
||||
# 余りが 2 の場合、何もしない
|
||||
return int(math.pow(3, a)) * 2
|
||||
# 余りが 0 の場合、何もしない
|
||||
return int(math.pow(3, a))
|
||||
|
||||
|
||||
"""ドライバーコード"""
|
||||
if __name__ == "__main__":
|
||||
n = 58
|
||||
|
||||
# 貪欲アルゴリズム
|
||||
res = max_product_cutting(n)
|
||||
print(f"切断の最大積は {res}")
|
||||
Reference in New Issue
Block a user