mirror of
https://github.com/krahets/hello-algo.git
synced 2026-09-04 14:17:14 +00:00
Translate all code to English (#1836)
* Review the EN heading format. * Fix pythontutor headings. * Fix pythontutor headings. * bug fixes * Fix headings in **/summary.md * Revisit the CN-to-EN translation for Python code using Claude-4.5 * Revisit the CN-to-EN translation for Java code using Claude-4.5 * Revisit the CN-to-EN translation for Cpp code using Claude-4.5. * Fix the dictionary. * Fix cpp code translation for the multipart strings. * Translate Go code to English. * Update workflows to test EN code. * Add EN translation for C. * Add EN translation for CSharp. * Add EN translation for Swift. * Trigger the CI check. * Revert. * Update en/hash_map.md * Add the EN version of Dart code. * Add the EN version of Kotlin code. * Add missing code files. * Add the EN version of JavaScript code. * Add the EN version of TypeScript code. * Fix the workflows. * Add the EN version of Ruby code. * Add the EN version of Rust code. * Update the CI check for the English version code. * Update Python CI check. * Fix cmakelists for en/C code. * Fix Ruby comments
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
// File: coin_change_greedy.go
|
||||
// Created Time: 2023-07-23
|
||||
// Author: Reanon (793584285@qq.com)
|
||||
|
||||
package chapter_greedy
|
||||
|
||||
/* Coin change: Greedy algorithm */
|
||||
func coinChangeGreedy(coins []int, amt int) int {
|
||||
// Assume coins list is sorted
|
||||
i := len(coins) - 1
|
||||
count := 0
|
||||
// Loop to make greedy choices until no remaining amount
|
||||
for amt > 0 {
|
||||
// Find the coin that is less than and closest to the remaining amount
|
||||
for i > 0 && coins[i] > amt {
|
||||
i--
|
||||
}
|
||||
// Choose coins[i]
|
||||
amt -= coins[i]
|
||||
count++
|
||||
}
|
||||
// If no feasible solution is found, return -1
|
||||
if amt != 0 {
|
||||
return -1
|
||||
}
|
||||
return count
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
// File: coin_change_greedy_test.go
|
||||
// Created Time: 2023-07-23
|
||||
// Author: Reanon (793584285@qq.com)
|
||||
|
||||
package chapter_greedy
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCoinChangeGreedy(t *testing.T) {
|
||||
// Greedy algorithm: Can guarantee finding the global optimal solution
|
||||
coins := []int{1, 5, 10, 20, 50, 100}
|
||||
amt := 186
|
||||
res := coinChangeGreedy(coins, amt)
|
||||
fmt.Printf("coins = %v, amt = %d\n", coins, amt)
|
||||
fmt.Printf("Minimum number of coins needed to make %d is %d\n", amt, res)
|
||||
|
||||
// Greedy algorithm: Cannot guarantee finding the global optimal solution
|
||||
coins = []int{1, 20, 50}
|
||||
amt = 60
|
||||
res = coinChangeGreedy(coins, amt)
|
||||
fmt.Printf("coins = %v, amt = %d\n", coins, amt)
|
||||
fmt.Printf("Minimum number of coins needed to make %d is %d\n", amt, res)
|
||||
fmt.Println("Actually the minimum number needed is 3, i.e., 20 + 20 + 20")
|
||||
|
||||
// Greedy algorithm: Cannot guarantee finding the global optimal solution
|
||||
coins = []int{1, 49, 50}
|
||||
amt = 98
|
||||
res = coinChangeGreedy(coins, amt)
|
||||
fmt.Printf("coins = %v, amt = %d\n", coins, amt)
|
||||
fmt.Printf("Minimum number of coins needed to make %d is %d\n", amt, res)
|
||||
fmt.Println("Actually the minimum number needed is 2, i.e., 49 + 49")
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
// File: fractional_knapsack.go
|
||||
// Created Time: 2023-07-23
|
||||
// Author: Reanon (793584285@qq.com)
|
||||
|
||||
package chapter_greedy
|
||||
|
||||
import "sort"
|
||||
|
||||
/* Item */
|
||||
type Item struct {
|
||||
w int // Item weight
|
||||
v int // Item value
|
||||
}
|
||||
|
||||
/* Fractional knapsack: Greedy algorithm */
|
||||
func fractionalKnapsack(wgt []int, val []int, cap int) float64 {
|
||||
// Create item list with two attributes: weight, value
|
||||
items := make([]Item, len(wgt))
|
||||
for i := 0; i < len(wgt); i++ {
|
||||
items[i] = Item{wgt[i], val[i]}
|
||||
}
|
||||
// Sort by unit value item.v / item.w from high to low
|
||||
sort.Slice(items, func(i, j int) bool {
|
||||
return float64(items[i].v)/float64(items[i].w) > float64(items[j].v)/float64(items[j].w)
|
||||
})
|
||||
// Loop for greedy selection
|
||||
res := 0.0
|
||||
for _, item := range items {
|
||||
if item.w <= cap {
|
||||
// If remaining capacity is sufficient, put the entire current item into the knapsack
|
||||
res += float64(item.v)
|
||||
cap -= item.w
|
||||
} else {
|
||||
// If remaining capacity is insufficient, put part of the current item into the knapsack
|
||||
res += float64(item.v) / float64(item.w) * float64(cap)
|
||||
// No remaining capacity, so break out of the loop
|
||||
break
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
// File: fractional_knapsack_test.go
|
||||
// Created Time: 2023-07-23
|
||||
// Author: Reanon (793584285@qq.com)
|
||||
|
||||
package chapter_greedy
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFractionalKnapsack(t *testing.T) {
|
||||
wgt := []int{10, 20, 30, 40, 50}
|
||||
val := []int{50, 120, 150, 210, 240}
|
||||
capacity := 50
|
||||
|
||||
// Greedy algorithm
|
||||
res := fractionalKnapsack(wgt, val, capacity)
|
||||
fmt.Println("Maximum item value not exceeding knapsack capacity is", res)
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
// File: max_capacity.go
|
||||
// Created Time: 2023-07-23
|
||||
// Author: Reanon (793584285@qq.com)
|
||||
|
||||
package chapter_greedy
|
||||
|
||||
import "math"
|
||||
|
||||
/* Max capacity: Greedy algorithm */
|
||||
func maxCapacity(ht []int) int {
|
||||
// Initialize i, j to be at both ends of the array
|
||||
i, j := 0, len(ht)-1
|
||||
// Initial max capacity is 0
|
||||
res := 0
|
||||
// Loop for greedy selection until the two boards meet
|
||||
for i < j {
|
||||
// Update max capacity
|
||||
capacity := int(math.Min(float64(ht[i]), float64(ht[j]))) * (j - i)
|
||||
res = int(math.Max(float64(res), float64(capacity)))
|
||||
// Move the shorter board inward
|
||||
if ht[i] < ht[j] {
|
||||
i++
|
||||
} else {
|
||||
j--
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
// File: max_capacity_test.go
|
||||
// Created Time: 2023-07-23
|
||||
// Author: Reanon (793584285@qq.com)
|
||||
|
||||
package chapter_greedy
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMaxCapacity(t *testing.T) {
|
||||
ht := []int{3, 8, 5, 2, 7, 7, 3, 4}
|
||||
|
||||
// Greedy algorithm
|
||||
res := maxCapacity(ht)
|
||||
fmt.Println("Maximum capacity is", res)
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
// File: max_product_cutting.go
|
||||
// Created Time: 2023-07-23
|
||||
// Author: Reanon (793584285@qq.com)
|
||||
|
||||
package chapter_greedy
|
||||
|
||||
import "math"
|
||||
|
||||
/* Max product cutting: Greedy algorithm */
|
||||
func maxProductCutting(n int) int {
|
||||
// When n <= 3, must cut out a 1
|
||||
if n <= 3 {
|
||||
return 1 * (n - 1)
|
||||
}
|
||||
// Greedily cut out 3, a is the number of 3s, b is the remainder
|
||||
a := n / 3
|
||||
b := n % 3
|
||||
if b == 1 {
|
||||
// When the remainder is 1, convert a pair of 1 * 3 to 2 * 2
|
||||
return int(math.Pow(3, float64(a-1))) * 2 * 2
|
||||
}
|
||||
if b == 2 {
|
||||
// When the remainder is 2, do nothing
|
||||
return int(math.Pow(3, float64(a))) * 2
|
||||
}
|
||||
// When the remainder is 0, do nothing
|
||||
return int(math.Pow(3, float64(a)))
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
// File: max_product_cutting_test.go
|
||||
// Created Time: 2023-07-23
|
||||
// Author: Reanon (793584285@qq.com)
|
||||
|
||||
package chapter_greedy
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMaxProductCutting(t *testing.T) {
|
||||
n := 58
|
||||
// Greedy algorithm
|
||||
res := maxProductCutting(n)
|
||||
fmt.Println("Maximum cutting product is", res)
|
||||
}
|
||||
Reference in New Issue
Block a user