classSolution { public: intmaxArea(vector<int>& height){ int n = height.size(); int l = 0,r =n-1, ret = 0; while(l<r){ ret = max(ret, (r-l)*min(height[l], height[r])); height[l] < height[r] ? ++l : --r; } return ret; } };
1 2 3 4 5 6 7 8 9 10 11 12 13
classSolution { public: intmaxArea(vector<int>& height){ int res = 0, i = 0, j = height.size() - 1; while (i < j) { int h = min(height[i], height[j]); res = max(res, h * (j - i)); while (i < j && h == height[i]) ++i; while (i < j && h == height[j]) --j; } return res; } };