mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-17 14:10:57 +00:00
d7b2277d2b
* Retranslate Japanese docs with GPT-5.4 * Retranslate Japanese code with GPT-5.4
28 lines
632 B
Go
28 lines
632 B
Go
// 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
|
|
}
|