LeetCode——11. Container With Most Water
一.题目链接:https://leetcode.com/problems/container-with-most-water/
二.题目大意:
给定n个非负整数a1,a2....an;其中每一个整数对应着一条垂直的线段,即(i,ai)到(i,0)这个线段。其中这n个线段中,任意两个线段与x轴就构成了一个容器;要你找出体积最大的容器出来,并返回它的体积值。
三.题解:
这道题目最容易想到的就是暴力法,从n个线段中任取两个来组成容器进行判断。
方法1:
暴力法,代码如下:
class Solution {
public:
int maxArea(vector<int>& height) {
int s = height.size();
int max_container = 0;
for(int i = 0 ; i < s; i++)
{
for(int j = i + 1; j < s; j++)
{
int temp = height[i] > height[j]?height[j]:height[i];
int temp_container = temp * (j - i);
if(temp_container > max_container)
max_container = temp_container;
}
}
return max_container;
}
};
该算法的时间复杂度为O(n2),空间复杂度为O(1),但是,最终结果会超时!
方法2:
仔细想想整个过程,我们会发现:对于两个长度不同的线段构成的容器,容器的体积取决于较短的那个线段。所以,我们可以利用这么一种方法:定义两个指针(L指针和R指针),分别指向数组的头部和尾部,然后每次比较L和R指向的线段长度大小,如果L指针指向的线段长度小的话,L指针向右移动;如果R指针指向的线段长度小的话,R指针向左移动。然后每次计算容器的体积,选择最大的那个作为最终结果。(至于方法为什么可行,在后面解释)代码如下:
class Solution {
public:
int maxArea(vector<int>& height) {
int c_len = height.size();
int max_container = 0;
int l_index = 0, r_index = c_len - 1;
while(l_index < r_index)
{
int temp = (r_index - l_index) * min(height[l_index],height[r_index]);
max_container = max_container>temp?max_container:temp;
if(height[l_index] < height[r_index])
l_index++;
else
r_index--;
}
return max_container;
}
};
该算法的时间复杂度为O(n),空间复杂度为O(1)。下面详细解释一下这种方法:
1.首先,要了解到的一点是此处的容器相当于一个两边不相同的长方形,而不是一个圆柱,所以它的体积计算公式是:底 x 高。而不是π x R2 x h 。
2.由于容器的体积是由较小的那个线段决定,假设L指针指向元素A,R指针指向元素B,如果A<B的话;那么,对于B左边所有的边(A除外),它们与A构成的容器体积一定小于A与B构成的容器体积。
证:假设A与B构成的容器的底为L1、容器的高为H1,A与B左边其他线段构成的容器的底为Li、高为Hi,则Li < L1一定成立,H1≥Hi也是一定成立的(因为体积取决于较小的线段,如果Hi > H1,则Hi = H1;否则Hi不变)所以Li x Hi一定小于L1 x H1。
所以当A<B的话,L指针向左移动,而不是R指针向右移动。这样就相当于,A不用再与B左边其他的线段配对了(相比暴力而言省去了很多没用的比较),这样的话每次判断哪个线段小,就移动相应的指针。
3.但是,如果A==B的话该怎么办?
实际上,当A==B的话,这种情况是不需要特别考虑的。即此时L指针向右移动也行,R指针向左移动也可以。为什么呢?因为此时的A和B都可以看作是较小的那个线段,根据第1步,L和R谁移动都是合理的。并且无论是L指针向右移动还是R指针向左移动,它们都遍历了A、B之间所有的线段,所以这两种移动情况结果是一样的。
LeetCode——11. Container With Most Water的更多相关文章
- leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II
11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...
- Leetcode 11. Container With Most Water(逼近法)
11. Container With Most Water Medium Given n non-negative integers a1, a2, ..., an , where each repr ...
- 如何装最多的水? — leetcode 11. Container With Most Water
炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...
- LeetCode 11. Container With Most Water (装最多水的容器)
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- [LeetCode] 11. Container With Most Water 装最多水的容器
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...
- LeetCode#11. Container With Most Water
问题描述 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ...
- Java [leetcode 11] Container With Most Water
问题描述: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ...
- C#解leetcode 11. Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- [LeetCode] 11. Container With Most Water My Submissions Question 解题思路
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- [leetcode]11. Container With Most Water存水最多的容器
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...
随机推荐
- CSS使用方法
CSS行内样式: 在开始标签内添加style样式属性 如:<p style="color:red;">内容</p> CSS内部样式: 内部样式(嵌入样式), ...
- BZOJ 5099: Pionek(双指针)(占位)
pro:有N个向量,你可以选择一些向量,使得其向量和离原点最远. 输出这个欧几里得距离的平方. sol:(感觉网上的证明都不是很充分,我自己也是半信半疑吧)日后证明了再补. #include<b ...
- CodeForces - 441E:Valera and Number (DP&数学期望&二进制)
Valera is a coder. Recently he wrote a funny program. The pseudo code for this program is given belo ...
- C++学习(十四)(C语言部分)之 数组
上期回顾:三大循环 for while do while (循环体至少会执行一次)四大跳转 goto continue(提前跳出当前循环 进入下一个循环) break(跳出本次循环) return(跳 ...
- Python知识点整理,基础3 - 字典操作
- 手把手教你用MATLAB画灰度直方图
hist =[1,2,3,4,5,6,7,8,7,6,5,4,3,2,1] %15个元素 这个行矩阵(数组)输入到命令行 bar(hist) 用列矩阵也行 hist_im=imhis ...
- linux忘记root密码
在选择系统界面选中要修改的系统(我的就是默认的第一个),按e建进入修改,在修改界面一直下到文件末尾,在末尾前一行左右,找到UTF-8那一行,在这一行敲一个空格,然后打init=/bin/sh 修改完成 ...
- nightwatchjs 基于nodejs&& webdriver 协议的自动化测试&&持续集成框架
nightwatchjs 是基于nodejs&& webdriver 协议的自动化测试&&持续集成框架 参考架构 参考资料 http://nightwatchjs.or ...
- Laya学习
IDE安装 https://www.jianshu.com/p/88fa76a5becc npm uninstall -g typescript npm install -g typescript@2 ...
- sublime 安装插件出现问题
一. 解决package Install不能安装 If for some reason the console installation instructions do not work for ...