Container With Most Water
2015-12-06 22:22
387 查看
题目: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.
思路:
本题和之前的 trapping rain water 没有较大关系,只是单纯的询问我们两根线连起来会不会有最大值。所以思路就是双指针法,两边开始,以最小值高度为长,做乘积。谁小谁往里走。
代码:
class Solution {public:
int maxArea(vector<int>& height) {
int i=0,j=height.size()-1;
// 1 2 3 4 5
int res=0,tem=0;
while(i<=j){
tem=(j-i)*min(height[i],height[j]);
if(height[i]>height[j]){
j--;
}else if(height[i]<height[j]){
i++;
}else{
i++;j--;
}
res=max(res,tem);
}
return res;//
}
};
相关文章推荐
- 解决HanziToPinyin中Collator.getAvailableLocales()在小米、HTC One等手机上获取本地语言环境不匹配转换拼音失败BUG
- poj1273 Drainage Ditches
- hdoj--1023 Train Problem II (高精度乘除法)
- [LeetCode]011-Container With Most Water
- Batch Normalizaion: Accelerating Deep Network Training by Reducing Internal Covariate Shift.
- Airport(未解决。。。)
- error = Error Domain=NSCocoaErrorDomain Code=3840
- 中文译文:Minerva-一种可扩展的高效的深度学习训练平台(Minerva - A Scalable and Highly Efficient Training Platform for Deep Learning)
- pod install 遇到 :[MT] DVTAssertions: ASSERTION FAILURE in ……
- activity_main.xml不能预览问题
- Repair Windows BCD
- 安装 composer SSL operation failed with code 1
- 产品 线上 保持 和 支持 服务 (Support and maintenance solutions)
- Mathematics:Raising Modulo Numbers(POJ 1995)
- 1090. Highest Price in Supply Chain (25)
- 1086. Tree Traversals Again (25)
- 1079. Total Sales of Supply Chain (25)
- 【Leetcode】Contains Duplicate II
- Brainman
- Bad Hair Day