mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-16 13:40:58 +00:00
Re-translate the Japanese version (#1871)
* Retranslate Japanese docs with GPT-5.4 * Retranslate Japanese code with GPT-5.4
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
|
||||
|
||||
/* コイン交換:貪欲法 */
|
||||
func coinChangeGreedy(coins []int, amt int) int {
|
||||
// coins リストはソート済みと仮定する
|
||||
i := len(coins) - 1
|
||||
count := 0
|
||||
// 残額がなくなるまで貪欲選択を繰り返す
|
||||
for amt > 0 {
|
||||
// 残額以下で最も近い硬貨を見つける
|
||||
for i > 0 && coins[i] > amt {
|
||||
i--
|
||||
}
|
||||
// coins[i] を選択する
|
||||
amt -= coins[i]
|
||||
count++
|
||||
}
|
||||
// 実行可能な解が見つからなければ -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) {
|
||||
// 貪欲法:大域最適解を保証できる
|
||||
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("%d を作るのに必要な最小硬貨枚数は %d\n", amt, res)
|
||||
|
||||
// 貪欲法:大域最適解を保証できない
|
||||
coins = []int{1, 20, 50}
|
||||
amt = 60
|
||||
res = coinChangeGreedy(coins, amt)
|
||||
fmt.Printf("coins = %v, amt = %d\n", coins, amt)
|
||||
fmt.Printf("%d を作るのに必要な最小硬貨枚数は %d\n", amt, res)
|
||||
fmt.Println("実際に必要な最小枚数は 3、つまり 20 + 20 + 20")
|
||||
|
||||
// 貪欲法:大域最適解を保証できない
|
||||
coins = []int{1, 49, 50}
|
||||
amt = 98
|
||||
res = coinChangeGreedy(coins, amt)
|
||||
fmt.Printf("coins = %v, amt = %d\n", coins, amt)
|
||||
fmt.Printf("%d を作るのに必要な最小硬貨枚数は %d\n", amt, res)
|
||||
fmt.Println("実際に必要な最小枚数は 2、つまり 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"
|
||||
|
||||
/* 品物 */
|
||||
type Item struct {
|
||||
w int // 品物の重さ
|
||||
v int // 品物の価値
|
||||
}
|
||||
|
||||
/* 分数ナップサック:貪欲法 */
|
||||
func fractionalKnapsack(wgt []int, val []int, cap int) float64 {
|
||||
// 重さと価値の 2 属性を持つ品物リストを作成
|
||||
items := make([]Item, len(wgt))
|
||||
for i := 0; i < len(wgt); i++ {
|
||||
items[i] = Item{wgt[i], val[i]}
|
||||
}
|
||||
// 単位価値 item.v / item.w の高い順にソートする
|
||||
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)
|
||||
})
|
||||
// 貪欲選択を繰り返す
|
||||
res := 0.0
|
||||
for _, item := range items {
|
||||
if item.w <= cap {
|
||||
// 残り容量が十分なら、現在の品物を丸ごとナップサックに入れる
|
||||
res += float64(item.v)
|
||||
cap -= item.w
|
||||
} else {
|
||||
// 残り容量が足りない場合は、現在の品物の一部だけをナップサックに入れる
|
||||
res += float64(item.v) / float64(item.w) * float64(cap)
|
||||
// 残り容量がないため、ループを抜ける
|
||||
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
|
||||
|
||||
// 貪欲法
|
||||
res := fractionalKnapsack(wgt, val, capacity)
|
||||
fmt.Println("ナップサック容量を超えない最大価値は", res)
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
// File: max_capacity.go
|
||||
// Created Time: 2023-07-23
|
||||
// Author: Reanon (793584285@qq.com)
|
||||
|
||||
package chapter_greedy
|
||||
|
||||
import "math"
|
||||
|
||||
/* 最大容量:貪欲法 */
|
||||
func maxCapacity(ht []int) int {
|
||||
// i, j を初期化し、それぞれ配列の両端に置く
|
||||
i, j := 0, len(ht)-1
|
||||
// 初期の最大容量は 0
|
||||
res := 0
|
||||
// 2 枚の板が出会うまで貪欲選択を繰り返す
|
||||
for i < j {
|
||||
// 最大容量を更新する
|
||||
capacity := int(math.Min(float64(ht[i]), float64(ht[j]))) * (j - i)
|
||||
res = int(math.Max(float64(res), float64(capacity)))
|
||||
// 短い方を内側へ動かす
|
||||
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}
|
||||
|
||||
// 貪欲法
|
||||
res := maxCapacity(ht)
|
||||
fmt.Println("最大容量は", 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"
|
||||
|
||||
/* 最大切断積:貪欲法 */
|
||||
func maxProductCutting(n int) int {
|
||||
// n <= 3 のときは、必ず 1 を切り出す
|
||||
if n <= 3 {
|
||||
return 1 * (n - 1)
|
||||
}
|
||||
// 貪欲に 3 を切り出し、a を 3 の個数、b を余りとする
|
||||
a := n / 3
|
||||
b := n % 3
|
||||
if b == 1 {
|
||||
// 余りが 1 のときは、1 * 3 を 2 * 2 に変える
|
||||
return int(math.Pow(3, float64(a-1))) * 2 * 2
|
||||
}
|
||||
if b == 2 {
|
||||
// 余りが 2 のときは、そのままにする
|
||||
return int(math.Pow(3, float64(a))) * 2
|
||||
}
|
||||
// 余りが 0 のときは、そのままにする
|
||||
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
|
||||
// 貪欲法
|
||||
res := maxProductCutting(n)
|
||||
fmt.Println("最大分割積は", res)
|
||||
}
|
||||
Reference in New Issue
Block a user