mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-20 18:52:36 +08:00
Retranslate the complete English edition from Simplified Chinese (#2010)
* Retranslate the complete English edition from Simplified Chinese * Polish three English explanations after reader-focused review
This commit is contained in:
@@ -6,43 +6,43 @@ Author: krahets (krahets@163.com)
|
||||
|
||||
|
||||
def coin_change_greedy(coins: list[int], amt: int) -> int:
|
||||
"""Coin change: Greedy algorithm"""
|
||||
# Assume coins list is sorted
|
||||
"""Coin change: greedy algorithm"""
|
||||
# Assume the coins list is sorted
|
||||
i = len(coins) - 1
|
||||
count = 0
|
||||
# Loop to make greedy choices until no remaining amount
|
||||
# Repeatedly make greedy choices until no amount remains
|
||||
while amt > 0:
|
||||
# Find the coin that is less than and closest to the remaining amount
|
||||
# Find the largest coin no greater 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 -1 if no feasible solution was found
|
||||
return count if amt == 0 else -1
|
||||
|
||||
|
||||
"""Driver Code"""
|
||||
"""Driver code"""
|
||||
if __name__ == "__main__":
|
||||
# Greedy algorithm: Can guarantee finding the global optimal solution
|
||||
# Greedy algorithm: guaranteed to find a globally 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 {amt} is {res}")
|
||||
print(f"Number of coins chosen by the greedy algorithm for amount {amt}: {res}")
|
||||
|
||||
# Greedy algorithm: Cannot guarantee finding the global optimal solution
|
||||
# Greedy algorithm: not guaranteed to find a globally 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 {amt} is {res}")
|
||||
print(f"Actually the minimum number needed is 3, i.e., 20 + 20 + 20")
|
||||
print(f"Number of coins chosen by the greedy algorithm for amount {amt}: {res}")
|
||||
print(f"The actual minimum is 3 coins: 20 + 20 + 20")
|
||||
|
||||
# Greedy algorithm: Cannot guarantee finding the global optimal solution
|
||||
# Greedy algorithm: not guaranteed to find a globally 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 {amt} is {res}")
|
||||
print(f"Actually the minimum number needed is 2, i.e., 49 + 49")
|
||||
print(f"Number of coins chosen by the greedy algorithm for amount {amt}: {res}")
|
||||
print(f"The actual minimum is 2 coins: 49 + 49")
|
||||
|
||||
@@ -14,27 +14,27 @@ class Item:
|
||||
|
||||
|
||||
def fractional_knapsack(wgt: list[int], val: list[int], cap: int) -> int:
|
||||
"""Fractional knapsack: Greedy algorithm"""
|
||||
# Create item list with two attributes: weight, value
|
||||
"""Fractional knapsack: greedy algorithm"""
|
||||
# Create a list of items, each with two attributes: weight and value
|
||||
items = [Item(w, v) for w, v in zip(wgt, val)]
|
||||
# Sort by unit value item.v / item.w from high to low
|
||||
# Sort by value per unit weight, item.v / item.w, in descending order
|
||||
items.sort(key=lambda item: item.v / item.w, reverse=True)
|
||||
# Loop for greedy selection
|
||||
# Repeatedly make greedy choices
|
||||
res = 0
|
||||
for item in items:
|
||||
if item.w <= cap:
|
||||
# If remaining capacity is sufficient, put the entire current item into the knapsack
|
||||
# If enough capacity remains, put the entire current item in the knapsack
|
||||
res += item.v
|
||||
cap -= item.w
|
||||
else:
|
||||
# If remaining capacity is insufficient, put part of the current item into the knapsack
|
||||
# If the remaining capacity is insufficient, put part of the current item in the knapsack
|
||||
res += (item.v / item.w) * cap
|
||||
# No remaining capacity, so break out of the loop
|
||||
# No capacity remains, so exit the loop
|
||||
break
|
||||
return res
|
||||
|
||||
|
||||
"""Driver Code"""
|
||||
"""Driver code"""
|
||||
if __name__ == "__main__":
|
||||
wgt = [10, 20, 30, 40, 50]
|
||||
val = [50, 120, 150, 210, 240]
|
||||
@@ -43,4 +43,4 @@ if __name__ == "__main__":
|
||||
|
||||
# Greedy algorithm
|
||||
res = fractional_knapsack(wgt, val, cap)
|
||||
print(f"The maximum item value without exceeding knapsack capacity is {res}")
|
||||
print(f"Maximum total value within the knapsack capacity: {res}")
|
||||
|
||||
@@ -6,17 +6,17 @@ Author: krahets (krahets@163.com)
|
||||
|
||||
|
||||
def max_capacity(ht: list[int]) -> int:
|
||||
"""Max capacity: Greedy algorithm"""
|
||||
# Initialize i, j to be at both ends of the array
|
||||
"""Maximum container capacity: greedy algorithm"""
|
||||
# Initialize i and j at opposite ends of the array
|
||||
i, j = 0, len(ht) - 1
|
||||
# Initial max capacity is 0
|
||||
# The initial maximum capacity is 0
|
||||
res = 0
|
||||
# Loop for greedy selection until the two boards meet
|
||||
# Repeatedly make greedy choices until the two sides meet
|
||||
while i < j:
|
||||
# Update max capacity
|
||||
# Update the maximum capacity
|
||||
cap = min(ht[i], ht[j]) * (j - i)
|
||||
res = max(res, cap)
|
||||
# Move the shorter board inward
|
||||
# Move the shorter side inward
|
||||
if ht[i] < ht[j]:
|
||||
i += 1
|
||||
else:
|
||||
@@ -24,10 +24,10 @@ def max_capacity(ht: list[int]) -> int:
|
||||
return res
|
||||
|
||||
|
||||
"""Driver Code"""
|
||||
"""Driver code"""
|
||||
if __name__ == "__main__":
|
||||
ht = [3, 8, 5, 2, 7, 7, 3, 4]
|
||||
|
||||
# Greedy algorithm
|
||||
res = max_capacity(ht)
|
||||
print(f"Max capacity is {res}")
|
||||
print(f"Maximum capacity: {res}")
|
||||
|
||||
@@ -8,26 +8,26 @@ import math
|
||||
|
||||
|
||||
def max_product_cutting(n: int) -> int:
|
||||
"""Max product cutting: Greedy algorithm"""
|
||||
# When n <= 3, must cut out a 1
|
||||
"""Maximum product after splitting: greedy algorithm"""
|
||||
# When n <= 3, one part must be 1
|
||||
if n <= 3:
|
||||
return 1 * (n - 1)
|
||||
# Greedily cut out 3, a is the number of 3s, b is the remainder
|
||||
# Greedily split off parts of size 3; a is the number of 3s and b is the remainder
|
||||
a, b = n // 3, n % 3
|
||||
if b == 1:
|
||||
# When the remainder is 1, convert a pair of 1 * 3 to 2 * 2
|
||||
# When the remainder is 1, replace one pair 1 * 3 with 2 * 2
|
||||
return int(math.pow(3, a - 1)) * 2 * 2
|
||||
if b == 2:
|
||||
# When the remainder is 2, do nothing
|
||||
# When the remainder is 2, no adjustment is needed
|
||||
return int(math.pow(3, a)) * 2
|
||||
# When the remainder is 0, do nothing
|
||||
# When the remainder is 0, no adjustment is needed
|
||||
return int(math.pow(3, a))
|
||||
|
||||
|
||||
"""Driver Code"""
|
||||
"""Driver code"""
|
||||
if __name__ == "__main__":
|
||||
n = 58
|
||||
|
||||
# Greedy algorithm
|
||||
res = max_product_cutting(n)
|
||||
print(f"Max product cutting is {res}")
|
||||
print(f"Maximum product after splitting: {res}")
|
||||
|
||||
Reference in New Issue
Block a user