mirror of
https://github.com/krahets/hello-algo.git
synced 2026-05-04 05:12:40 +08:00
build
This commit is contained in:
@@ -157,9 +157,39 @@ status: new
|
||||
=== "Go"
|
||||
|
||||
```go title="fractional_knapsack.go"
|
||||
[class]{}-[func]{II}
|
||||
/* 物品 */
|
||||
type Item struct {
|
||||
w int // 物品重量
|
||||
v int // 物品价值
|
||||
}
|
||||
|
||||
[class]{}-[func]{fractionalKnapsack}
|
||||
/* 分数背包:贪心 */
|
||||
func fractionalKnapsack(wgt []int, val []int, cap int) float64 {
|
||||
// 创建物品列表,包含两个属性:重量、价值
|
||||
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
|
||||
}
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
|
||||
@@ -95,7 +95,27 @@ status: new
|
||||
=== "Go"
|
||||
|
||||
```go title="coin_change_greedy.go"
|
||||
[class]{}-[func]{coinChangeGreedy}
|
||||
/* 零钱兑换:贪心 */
|
||||
func coinChangeGreedy(coins []int, amt int) int {
|
||||
// 假设 coins 列表有序
|
||||
i := len(coins) - 1
|
||||
count := 0
|
||||
// 循环进行贪心选择,直到无剩余金额
|
||||
for amt > 0 {
|
||||
// 找到小于且最接近剩余金额的硬币
|
||||
for coins[i] > amt {
|
||||
i--
|
||||
}
|
||||
// 选择 coins[i]
|
||||
amt -= coins[i]
|
||||
count++
|
||||
}
|
||||
// 若未找到可行方案,则返回 -1
|
||||
if amt != 0 {
|
||||
return -1
|
||||
}
|
||||
return count
|
||||
}
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
|
||||
@@ -161,7 +161,26 @@ $$
|
||||
=== "Go"
|
||||
|
||||
```go title="max_capacity.go"
|
||||
[class]{}-[func]{maxCapacity}
|
||||
/* 最大容量:贪心 */
|
||||
func maxCapacity(ht []int) int {
|
||||
// 初始化 i, j 分列数组两端
|
||||
i, j := 0, len(ht)-1
|
||||
// 初始最大容量为 0
|
||||
res := 0
|
||||
// 循环贪心选择,直至两板相遇
|
||||
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
|
||||
}
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
|
||||
@@ -147,7 +147,26 @@ $$
|
||||
=== "Go"
|
||||
|
||||
```go title="max_product_cutting.go"
|
||||
[class]{}-[func]{maxProductCutting}
|
||||
/* 最大切分乘积:贪心 */
|
||||
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)))
|
||||
}
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
|
||||
Reference in New Issue
Block a user