[leetcode]Container With Most Water
#include <iostream> #include <vector> using namespace std; class Solution { public: int maxArea(vector<int> &height) { // Start typing your C/C++ solution below // DO NOT write int main() function if (height.size() < 2) return 0; int l = 0; int r = height.size() - 1; int lCurrMax = height[l]; int rCurrMax = height[r]; int areaMax = (r-l)*min(lCurrMax, rCurrMax); while (l+1<r){ if (lCurrMax < rCurrMax){ if (height[l+1] <= height[l]){ l++; } else{ l++; lCurrMax = height[l]; areaMax = max(areaMax, (r-l)*min(height[l], height[r])); } } else{ if (height[r-1] <= height[r]){ r--; } else{ r--; rCurrMax = height[r]; areaMax = max(areaMax, (r-l)*min(height[l], height[r])); } } } return areaMax; } }; int main() { vector<int> height; height.push_back(1); height.push_back(2); height.push_back(4); height.push_back(3); Solution s; int areaMax = s.maxArea(height); return 0; }
EOF