mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-01 20:12:07 +08:00
translation: Add Python and Java code for EN version (#1345)
* Add the intial translation of code of all the languages * test * revert * Remove * Add Python and Java code for EN version
This commit is contained in:
48
en/codes/python/chapter_greedy/coin_change_greedy.py
Normal file
48
en/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:
|
||||
"""Coin change: Greedy"""
|
||||
# Assume coins list is ordered
|
||||
i = len(coins) - 1
|
||||
count = 0
|
||||
# Loop for greedy selection until no remaining amount
|
||||
while amt > 0:
|
||||
# Find the smallest coin close to and less than the remaining amount
|
||||
while i > 0 and coins[i] > amt:
|
||||
i -= 1
|
||||
# Choose coins[i]
|
||||
amt -= coins[i]
|
||||
count += 1
|
||||
# If no feasible solution is found, return -1
|
||||
return count if amt == 0 else -1
|
||||
|
||||
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
# Greedy: can ensure finding a global optimal solution
|
||||
coins = [1, 5, 10, 20, 50, 100]
|
||||
amt = 186
|
||||
res = coin_change_greedy(coins, amt)
|
||||
print(f"\ncoins = {coins}, amt = {amt}")
|
||||
print(f"The minimum number of coins needed to make up {amt} is {res}")
|
||||
|
||||
# Greedy: cannot ensure finding a global optimal solution
|
||||
coins = [1, 20, 50]
|
||||
amt = 60
|
||||
res = coin_change_greedy(coins, amt)
|
||||
print(f"\ncoins = {coins}, amt = {amt}")
|
||||
print(f"The minimum number of coins needed to make up {amt} is {res}")
|
||||
print(f"In reality, the minimum number needed is 3, i.e., 20 + 20 + 20")
|
||||
|
||||
# Greedy: cannot ensure finding a global optimal solution
|
||||
coins = [1, 49, 50]
|
||||
amt = 98
|
||||
res = coin_change_greedy(coins, amt)
|
||||
print(f"\ncoins = {coins}, amt = {amt}")
|
||||
print(f"The minimum number of coins needed to make up {amt} is {res}")
|
||||
print(f"In reality, the minimum number needed is 2, i.e., 49 + 49")
|
||||
46
en/codes/python/chapter_greedy/fractional_knapsack.py
Normal file
46
en/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:
|
||||
"""Item"""
|
||||
|
||||
def __init__(self, w: int, v: int):
|
||||
self.w = w # Item weight
|
||||
self.v = v # Item value
|
||||
|
||||
|
||||
def fractional_knapsack(wgt: list[int], val: list[int], cap: int) -> int:
|
||||
"""Fractional knapsack: Greedy"""
|
||||
# Create an item list, containing two properties: weight, value
|
||||
items = [Item(w, v) for w, v in zip(wgt, val)]
|
||||
# Sort by unit value item.v / item.w from high to low
|
||||
items.sort(key=lambda item: item.v / item.w, reverse=True)
|
||||
# Loop for greedy selection
|
||||
res = 0
|
||||
for item in items:
|
||||
if item.w <= cap:
|
||||
# If the remaining capacity is sufficient, put the entire item into the knapsack
|
||||
res += item.v
|
||||
cap -= item.w
|
||||
else:
|
||||
# If the remaining capacity is insufficient, put part of the item into the knapsack
|
||||
res += (item.v / item.w) * cap
|
||||
# No remaining capacity left, thus break the loop
|
||||
break
|
||||
return res
|
||||
|
||||
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
wgt = [10, 20, 30, 40, 50]
|
||||
val = [50, 120, 150, 210, 240]
|
||||
cap = 50
|
||||
n = len(wgt)
|
||||
|
||||
# Greedy algorithm
|
||||
res = fractional_knapsack(wgt, val, cap)
|
||||
print(f"The maximum item value without exceeding knapsack capacity is {res}")
|
||||
33
en/codes/python/chapter_greedy/max_capacity.py
Normal file
33
en/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:
|
||||
"""Maximum capacity: Greedy"""
|
||||
# Initialize i, j, making them split the array at both ends
|
||||
i, j = 0, len(ht) - 1
|
||||
# Initial maximum capacity is 0
|
||||
res = 0
|
||||
# Loop for greedy selection until the two boards meet
|
||||
while i < j:
|
||||
# Update maximum capacity
|
||||
cap = min(ht[i], ht[j]) * (j - i)
|
||||
res = max(res, cap)
|
||||
# Move the shorter board inward
|
||||
if ht[i] < ht[j]:
|
||||
i += 1
|
||||
else:
|
||||
j -= 1
|
||||
return res
|
||||
|
||||
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
ht = [3, 8, 5, 2, 7, 7, 3, 4]
|
||||
|
||||
# Greedy algorithm
|
||||
res = max_capacity(ht)
|
||||
print(f"Maximum capacity is {res}")
|
||||
33
en/codes/python/chapter_greedy/max_product_cutting.py
Normal file
33
en/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:
|
||||
"""Maximum product of cutting: Greedy"""
|
||||
# When n <= 3, must cut out a 1
|
||||
if n <= 3:
|
||||
return 1 * (n - 1)
|
||||
# Greedy cut out 3s, a is the number of 3s, b is the remainder
|
||||
a, b = n // 3, n % 3
|
||||
if b == 1:
|
||||
# When the remainder is 1, convert a pair of 1 * 3 into 2 * 2
|
||||
return int(math.pow(3, a - 1)) * 2 * 2
|
||||
if b == 2:
|
||||
# When the remainder is 2, do nothing
|
||||
return int(math.pow(3, a)) * 2
|
||||
# When the remainder is 0, do nothing
|
||||
return int(math.pow(3, a))
|
||||
|
||||
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
n = 58
|
||||
|
||||
# Greedy algorithm
|
||||
res = max_product_cutting(n)
|
||||
print(f"Maximum product of cutting is {res}")
|
||||
Reference in New Issue
Block a user