leetcode_11. Container With Most Water
一,问题:
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)处的一个点。 绘制n条垂直线,使得线i的两个端点处于(i,ai)和(i,0)处。 找到两条线,它们与x轴一起形成一个容器,以使容器包含最多的水。
注意:您不得倾斜容器(即木板效应),并且n至少为2。
二,思路:
1,暴力法:直接通过双重循环遍历,找出结果。
2,等值线法:其实还是针对法的翻版,找出n1对应的n2等值线,从而针对找寻对应获得最大值的n2。
三,代码:
1.V1:
func maxArea(height []int) int { maxarea:=0 for k:=0;k<len(height)-1;k++{ for k2:=k+1;k2<len(height);k2++{ minheight:=height[k] if height[k2]<height[k]{ minheight=height[k2] } smaxarea:=(k2-k)*int(minheight) if smaxarea>maxarea{ maxarea=smaxarea } } } return maxarea }