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越大,容量也越大

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容