题解 | #盛水最多的容器#
盛水最多的容器
https://www.nowcoder.com/practice/3d8d6a8e516e4633a2244d2934e5aa47
package main
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param height int整型一维数组
* @return int整型
*/
// 两边双指针;滑动窗口;贪心计算
//比较难想的是滑动窗口移动方式;从两边往中间靠拢
func maxArea(height []int) int {
// write code here
min2Int := func(a, b int) int {
if a < b {
return a
}
return b
}
res := 0
left, right := 0, len(height)-1
for left < right {
tmp := (right - left) * min2Int(height[left], height[right])
if res < tmp {
res = tmp
}
//移动窗口?
//优先移动边小的
if height[left] < height[right] {
left++
} else {
right--
}
}
return res
}
联想公司福利 1477人发布
查看4道真题和解析