【Leetcode】11. Container With Most Water


Given n non-negative integers a1a2, ..., an , where each represents a point at coordinate (iai). n vertical lines are drawn such that the two endpoints of line i is at (iai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.

1 遍历的时候以width为变量

2 L指向左边,R指向右边,如果height[L]<height[R], 如果移动高的那个,也就是左移R,有以下情况:

(1)左移后,height[R]<height[L],则容器变小

(2)左移后,height[R]>height[L],则容器也变小

(3)左移后,height[R]=height[L],因为底变短了,则容器也变小

   所以移动矮的那个指针才有用


1 基本思路是双指针,每一次遍历都更新water数,对于左右指针,每次移动矮的那个,木桶原理

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

推荐阅读更多精彩内容