This commit is contained in:
krahets
2024-05-15 19:00:27 +08:00
parent bd54cd096b
commit e434a3343c
36 changed files with 402 additions and 107 deletions

View File

@@ -397,7 +397,28 @@ $$
=== "Ruby"
```ruby title="max_capacity.rb"
[class]{}-[func]{max_capacity}
### 最大容量:贪心 ###
def max_capacity(ht)
# 初始化 i, j使其分列数组两端
i, j = 0, ht.length - 1
# 初始最大容量为 0
res = 0
# 循环贪心选择,直至两板相遇
while i < j
# 更新最大容量
cap = [ht[i], ht[j]].min * (j - i)
res = [res, cap].max
# 向内移动短板
if ht[i] < ht[j]
i += 1
else
j -= 1
end
end
res
end
```
=== "Zig"