leetcode:Container With Most Water [11]

来源:互联网 发布:网络词汇洪荒之力意思 编辑:程序博客网 时间:2024/05/07 23:07

Given n non-negative integers a1a2, ..., an, where each represents a point at coordinate (iai). n vertical lines are drawn such that the two endpoints of line i is at (iai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.


class Solution {private:int min(int a, int b) {return a < b ? a : b;}public:int maxArea(vector<int>& height) {int max = 0;int i = 0, j = height.size() - 1;while (i < j) {int Area = (j - i)*min(height[i], height[j]);if (Area > max) max = Area;if (height[i] < height[j]) i++;else j--;}return max;}};



0 0