LeetCode 11 容器盛水问题
来源:互联网 发布:月度m2数据 编辑:程序博客网 时间:2024/04/29 11:52
老办法遍历肯定是超时 无需尝试.
解决思路为取两侧的挡板,高为a,b.因为是两侧了,所以距离肯定是最远的.接下来从a,b中高度低的一侧向里找,遇到比a高的就比较容积,当a,b大小发生改变时候重新判断,保证遍历方向是从a,b中低的一侧向里走就好.
c# code
public class Solution
{
public int MaxArea(int[] height)
{
if(height.Length<2)
return 0;
int max=Math.Min(height[0],height[height.Length-1])*(height.Length-1),i=0,j=height.Length-1;
int a=height[i],b=height[j];
while(i<j)
{
bool flag=a<b;
if(flag==true)
{
i++;
if(height[i]>a)
{
a=height[i];
max=max>Math.Min(a,b)*(j-i)?max:Math.Min(a,b)*(j-i);
}
}
else
{
j--;
if(height[j]>b)
{
b=height[j];
max=max>Math.Min(a,b)*(j-i)?max:Math.Min(a,b)*(j-i);
}
}
}
return max;
}
}
- LeetCode 11 容器盛水问题
- leetcode:最大容器问题
- Leetcode trapping rain water 容器装水问题
- 【LeetCode】容器装水
- 11. 坐标轴容器盛水问题
- 最大盛水容器问题_M
- 第11题 容器装水问题
- [LeetCode] 最大容器问题 Container with Most Water
- LeetCode-11 Container With Most Water(容器装水最大量)
- leetcode笔记—容纳最多水的容器
- LeetCode 11. Container With Most Water(容器装水)
- LeetCode 11 Container With Most Water(最大水容器)
- LeetCode #11 Container With Most Water 最大容器 解题小节
- leetcode桶装水问题
- 容器装载数据问题
- list容器排序问题?
- 容器迭代器失效问题
- 容器sort排序问题
- 哈夫曼编/译码器
- LeetCode 10 正则表达式匹配
- 对实体—联系模型的基本概念的认识
- JLabel类的使用
- 正则表达式学习之邮箱匹配
- LeetCode 11 容器盛水问题
- HDU 1335 Basically Speaking
- 人生重要的道路:软工
- 海选女主角
- Android Studio的下载及安装
- HDU 5439 Aggregated Counting (2015年长春赛区网络赛C题)
- 【A/B%m+扩展欧几里得】hdu 1576 A/B
- 0916Android基础自定义View进度条专题
- 读《“问题导学”模式英语学习兴趣的培养》有感