leetcode 11. Container With Most Water
2016-04-27 09:41
369 查看
题目:
Given n non-negative integers a1, a2,
..., an, where each represents a point at coordinate (i, ai). n vertical
lines are drawn such that the two endpoints of line i is 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.
描述:
提供了n个非负的整数a1, a2,
..., an,每个代表了一个在坐标系中的点(i, ai).这n个点与相对的点(i,
0)构成了垂直于x轴的直线。找出其中两根直线,使得它们之间围成的谁容量最大。
每条垂直于x轴的线,两个端点是(i,ai)(i,0),每两条直线之间相距为n个1的距离,那么两条直线之间的容量为高度较小的直线长度*(l2-l1),一个矩形大小。
令l1指向height[0],l2指向height[1],假设l2>l1,那么移动向左移动l2,就算l2变大,但是l1小,还是无法改变最大容量。l1 l2是否向中间移动,取决于移动后的容量。
也就是说,每次移动头部变量i和尾部变量j中的一个,哪个变量的高度小,就把这个变量向中心移动。计算此时的装水量并且和最大装水量的当前值做比较。
int maxArea(int* height, int heightSize) {
int i,j;
int l1,l2,tem;//l1,l2分别为选取的左右两根直线,它俩之间可以包含多条直线
int mwater=0;//水容量,初始化为0
int twater=0;
//令l1指向height[0],l2指向height[1],假设l2>l1,那么移动向左移动l2,就算l2变大,但是l1小,还是无法改变最大容量
//l1 l2是否向中间移动,取决于移动后的容量。
//也就是说,每次移动头部变量i和尾部变量j中的一个,哪个变量的高度小,就把这个变量向中心移动。
i=0;
j=heightSize-1;
while(i<j)
{
l1=height[i];
l2=height[j];
tem=l1>l2?l2:l1;
//水容量
twater=tem*(j-i);
if(twater>mwater)
{
mwater=twater;
}
if(l1<l2)
i++;
else
j--;
}
return mwater;
}
Given n non-negative integers a1, a2,
..., an, where each represents a point at coordinate (i, ai). n vertical
lines are drawn such that the two endpoints of line i is 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.
描述:
提供了n个非负的整数a1, a2,
..., an,每个代表了一个在坐标系中的点(i, ai).这n个点与相对的点(i,
0)构成了垂直于x轴的直线。找出其中两根直线,使得它们之间围成的谁容量最大。
每条垂直于x轴的线,两个端点是(i,ai)(i,0),每两条直线之间相距为n个1的距离,那么两条直线之间的容量为高度较小的直线长度*(l2-l1),一个矩形大小。
令l1指向height[0],l2指向height[1],假设l2>l1,那么移动向左移动l2,就算l2变大,但是l1小,还是无法改变最大容量。l1 l2是否向中间移动,取决于移动后的容量。
也就是说,每次移动头部变量i和尾部变量j中的一个,哪个变量的高度小,就把这个变量向中心移动。计算此时的装水量并且和最大装水量的当前值做比较。
int maxArea(int* height, int heightSize) {
int i,j;
int l1,l2,tem;//l1,l2分别为选取的左右两根直线,它俩之间可以包含多条直线
int mwater=0;//水容量,初始化为0
int twater=0;
//令l1指向height[0],l2指向height[1],假设l2>l1,那么移动向左移动l2,就算l2变大,但是l1小,还是无法改变最大容量
//l1 l2是否向中间移动,取决于移动后的容量。
//也就是说,每次移动头部变量i和尾部变量j中的一个,哪个变量的高度小,就把这个变量向中心移动。
i=0;
j=heightSize-1;
while(i<j)
{
l1=height[i];
l2=height[j];
tem=l1>l2?l2:l1;
//水容量
twater=tem*(j-i);
if(twater>mwater)
{
mwater=twater;
}
if(l1<l2)
i++;
else
j--;
}
return mwater;
}
相关文章推荐
- fn contains 包含字符串 三种方法判断
- Troubleshooting 'library cache: mutex X' waits. (文档 ID 1357946.1)
- 习近平主席关心人工智能事业的发展
- WAITEVENT: "library cache: mutex X" (文档 ID 727400.1)
- RAID的介绍和RAID的安装
- HDU 1302 The Snail(模拟题)
- 最新型的TurboMail智慧邮件服务器平台
- 先搜集下关于行动列表和行为树的AI制作
- 10252_Common_permutaion
- 关于服务器raid的一个记录
- 关于服务器raid的一个记录
- LeetCode *** 220. Contains Duplicate III (set::lower_bound)
- 11. Container With Most Water
- int main(int argc,char* argv[])参数简介
- Shell 括号辨识(转http://blog.csdn.net/taiyang1987912/article/details/39551385)
- AIX查看端口被占用
- 自定义控件三部曲之绘图篇(九)——Paint之setColorFilter
- POJ 1681 Painter's Problem (高斯消元)
- LeeCode(Database)-Duplicate Emails
- lightoj1070 - Algebraic Problem(Matrix)