社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
参考 https://leetcode-cn.com/problems/container-with-most-water/
package main
import "fmt"
var (
max int
l int
r int
)
func main() {
max = 0
l = 0
height := [9]int{1, 8, 6, 2, 5, 4, 8, 3, 7}
r = len(height) - 1
// 从两端开始
for l < r {
temp := 0
if height[l] > height[r] {
temp = height[r] * (r - l)
} else {
temp = height[l] * (r - l)
}
if temp > max {
max = temp
}
// 容积,有短板决定,所以不断查找更长的板
if height[l] > height[r] {
r--
} else {
l++
}
}
fmt.Println("max area : ", max)
}
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!