Golang Leetcode 11. Container With Most Water.go

版权声明:原创勿转 https://blog.csdn.net/anakinsun/article/details/89067141

思路

面积等于:小于等于此端的高*小于等于当前的最大区间长度

code


func maxArea(height []int) int {
	left, right := 0, len(height)-1
	maxArea := 0
	for left < right {
		maxArea = max(maxArea, min(height[left], height[right])*(right-left))
		if height[left] < height[right] {
			left++
		} else {
			right--
		}
	}
	return maxArea
}

func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}
func min(x, y int) int {
	if x > y {
		return y
	}
	return x
}

更多内容请移步我的repo:https://github.com/anakin/golang-leetcode

猜你喜欢

转载自blog.csdn.net/anakinsun/article/details/89067141