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.
给定一个数组,每个索引代表一面障碍物,索引位的值代表障碍物的高度;从数组中选取两个障碍物,要求两个障碍物和底构成的槽能装水的量最大。
class Solution { public: int maxArea(vector<int> &height) { int result=0; int size=height.size(); if(size==0)return result; int phead=0; int ptail=size-1; while(phead<ptail){ //计算当前盛水量 int shorter=min(height[phead], height[ptail]); int volumn=shorter*(ptail-phead); if(volumn>result)result=volumn; //搜索新的可能的水槽边界 if(height[phead]<height[ptail]){ while(phead<ptail && height[phead]<=shorter)phead++; } else{ while(phead<ptail && height[ptail]<=shorter)ptail--; } } return result; } };
LeetCode 011 Container With Most Water,布布扣,bubuko.com
LeetCode 011 Container With Most Water
原文地址:http://blog.csdn.net/harryhuang1990/article/details/25918893