您的位置:首页 > 大数据 > 人工智能

Leetcode Container With Most Water

2016-12-19 14:08 357 查看
题意:找到蓄水量最大的容器。

思路:从两边开始找,每次更新较小的边。

class Solution {
public:
int maxArea(vector<int>& height) {
int low = 0;
int high = height.size() - 1;
int maxwater = 0;
while(low < high) {
maxwater = max(maxwater, (high - low) * min(height[low], height[high]));
if(height[low] > height[high]) high --;
else low ++;
}
return maxwater;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode greedy