mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-26 11:32:31 +08:00
build
This commit is contained in:
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user