Leetcode 链接
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
Note: You may not slant the container and n is at least 2.
给出 n 个正整数 a1, a2, ..., an, 每个数代表坐标 (i, ai). 任意两个点向 x 轴引垂直线后可以与 x 轴形成一个能装水的容器。求该容器的最大容量。
func min(a, b int) int { if a < b { return a } return b } func max(a, b int) int{ if a > b { return a } return b } func maxArea(height []int) int { left := 0 right := len(height) - 1 res := -1 for left < right { h := min(height[left], height[right]) res = max(res, h * (right-left)) for (left < right) && (height[left] <= h) { left ++ } for (left < right) && (height[right] <= h){ right -- } } return res }
Copyright© 2013-2020
All Rights Reserved 京ICP备2023019179号-8