leetcode 11. Container With Most Water

Givennnon-negative integersa1,a2, ...,an, where each represents a point at coordinate (i,ai).nvertical lines are drawn such that the two endpoints of lineiis 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 andnis at least 2.

分析:

题目大意是要找出组成容器的最大容量,容量的大小计算公式: area = (H[j]-H[i]) * min(H[i], H[j])    (i<=j)

方法:容量大小取决于两根线的距离[X]和两根线当中小的那个min。

可以考虑将 [X] 初始化为最大值,然后不断递减,当固定[X]时,min越大,容量也越大

你可能感兴趣的:(leetcode 11. Container With Most Water)