首页 > 代码库 > leetcode-Container With Most Water-11

leetcode-Container With Most Water-11

输入一个数组,数组中的值代表在数轴的i位置有一个高位height[i]的板子,求两个板子间盛水的最大值。

用两个指针,指向头尾,计算面积,然后移动矮的那个指针,ON

 1 class Solution { 2 public: 3     int maxArea(vector<int>& height) { 4         if(height.size()<=1) return 0; 5         int i=0,j=height.size()-1; 6         int ans=0; 7         while(i<j){ 8             int tmp=(j-i)*min(height[i],height[j]); 9             ans=max(ans,tmp);10             if(height[i]<=height[j]) i++;11             else j--;12         }13         return ans;14     }15 };

 

leetcode-Container With Most Water-11