This commit is contained in:
krahets
2024-03-25 22:43:12 +08:00
parent 22017aa8e5
commit 87af663929
70 changed files with 7428 additions and 32 deletions

View File

@@ -368,6 +368,32 @@ $$
}
```
=== "Kotlin"
```kotlin title="max_capacity.kt"
/* 最大容量:贪心 */
fun maxCapacity(ht: IntArray): Int {
// 初始化 i, j使其分列数组两端
var i = 0
var j = ht.size - 1
// 初始最大容量为 0
var res = 0
// 循环贪心选择,直至两板相遇
while (i < j) {
// 更新最大容量
val cap = (min(ht[i].toDouble(), ht[j].toDouble()) * (j - i)).toInt()
res = max(res.toDouble(), cap.toDouble()).toInt()
// 向内移动短板
if (ht[i] < ht[j]) {
i++
} else {
j--
}
}
return res
}
```
=== "Zig"
```zig title="max_capacity.zig"