[LeetCode] Container With Most Water

简介: Well, an interesting problem. If you draw some examples on a white board and try to figure out the regularities, you may have noticed that the key to ...

Well, an interesting problem. If you draw some examples on a white board and try to figure out the regularities, you may have noticed that the key to solving this problem is to use two pointers, one starting from the left end and the other starting from the right end. We compute an area for one configuration of the two pointers. Then we need to move them. If the left pointer has lower height, then we know it must be moved to one point with larger height in the right to make it possible for the area to increase. For the right pointer, the case is similar.

The following code is not well optimized, but being pretty short :-)

 1 class Solution {
 2 public:
 3     int maxArea(vector<int> &height) {
 4         int l = 0, r = height.size() - 1, area = 0;
 5         while(l < r) {
 6             area = max(area, min(height[r], height[l]) * (r - l));  
 7             height[l] <= height[r] ? l++ : r--;
 8         }
 9         return area;
10     }
11 };

You may avoid unnecessary checks by moving the left and right pointers to another place with larger height. However, the following code runs slower than the above one...

 1 class Solution {
 2 public:
 3     int maxArea(vector<int> &height) {
 4         int l = 0, r = height.size() - 1, area = 0;
 5         while(l < r) {
 6             int h = min(height[l], height[r]), w = r - l;
 7             area = max(area, h * w);  
 8             while (height[l] <= h && l < r) l++;
 9             while (height[r] <= h && r > l) r--;
10         }
11         return area;
12     }
13 };

Since the problem is not that hard, you may be required to think more, such at its time complexity. Could you prove that it runs in O(n) time? Well, this link has a very clever proof.

目录
相关文章
|
人工智能 容器
Leetcode 11. Container With Most Water
题目可以这么理解,在i位置有条高为ai的竖线,让你选出两台竖线构成一个容器,使得该容器装的水最多,注意容器不能倾斜。
56 3
Leetcode 365. Water and Jug Problem
一句话理解题意:有容积为x和y升的俩水壶,能不能量出z升的水。 我刚开始看到这题,立马就想了下暴力搜索的可能性,但考虑了下数据大小,立马放弃这个暴力的想法,于是意识到肯定有比较简单的数学方法,其实我自己没想到,后来看还是看了别人的代码,很多博客都直接给出了解法, 但没介绍为什么能这么解。所以我决定解释下我自己的思路。
58 0
LeetCode 417. Pacific Atlantic Water Flow
给定一个 m x n 的非负整数矩阵来表示一片大陆上各个单元格的高度。“太平洋”处于大陆的左边界和上边界,而“大西洋”处于大陆的右边界和下边界。 规定水流只能按照上、下、左、右四个方向流动,且只能从高到低或者在同等高度上流动。 请找出那些水流既可以流动到“太平洋”,又能流动到“大西洋”的陆地单元的坐标。
117 0
LeetCode 417. Pacific Atlantic Water Flow
LeetCode 407. Trapping Rain Water II
我们把最外面的四边当成四面墙,想象海水面不断的升高,首先会浸过墙面最低的格子,如果墙面最低格子的四周(出了在墙面的格子)有比它矮的格子,那么这就可以形成一个蓄水池,蓄水池的最高高度就是墙面最低的格子,于是我们计算这个蓄水池可以获得的蓄水量;然后这个蓄水池被添加到墙面中;继续在墙面中找最低的格子;
108 0
LeetCode 407. Trapping Rain Water II
|
索引
LeetCode 42 Trapping Rain Water
给定n个非负整数,每个非负整数表示一个宽度为1的柱子,计算下雨后能够捕获多少水.
76 0
LeetCode 42 Trapping Rain Water
|
机器学习/深度学习 PHP 索引
[Leetcode 之 PHP 解析 (42. Trapping Rain Water)
[Leetcode 之 PHP 解析 (42. Trapping Rain Water)
112 0
[Leetcode 之 PHP 解析 (42. Trapping Rain Water)
|
容器
Leetcode-Medium 11. Container With Most Water
Leetcode-Medium 11. Container With Most Water
104 0
Leetcode-Medium 11. Container With Most Water
|
数据库
LeetCode(数据库)- The Airport With the Most Traffic
LeetCode(数据库)- The Airport With the Most Traffic
138 0
LeetCode - 42. Trapping Rain Water
42. Trapping Rain Water  Problem's Link  ---------------------------------------------------------------------------- Mean:  在坐标上给你一些竖直放置的条形积木,问你这个积木能够容纳多少液体.
904 0