# 7 Container With Most Water_Leetcode

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.

container的面积由两个因素组成，一个是高度，一个是宽度。

Code:

class Solution {
public:
int maxArea(vector<int> &height) {
int n = height.size();
if(n < 2) return 0;
int res = 0;

int left = 0, right = n-1;
while(left < right)
{
int tmp = min(height[left], height[right]) * (right - left);
if(tmp > res) res = tmp;

if(height[left] < height[right]) left++;
else right--;
}
return res;
}
};


posted @ 2014-10-16 21:56  Avril  阅读(285)  评论(0编辑  收藏