intmaxArea(vector<int>& height){ int len = height.size(); intmax = 0; for (int i = 0; i < len-1; ++i){ for (int j = i+1; j < len; ++j){ int sum = (height[i] < height[j] ? height[i]*(j-i) : height[j]*(j-i)); max = max > sum ? max : sum; } } returnmax; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//O(n)
intmaxArea(vector<int>& height){ int len = height.size(); intmax = 0; int left = 0, right = len-1; while (left < right){ int sum = (height[left] < height[right] ? height[left]*(right-left) : height[right]*(right-left)); max = max > sum ? max : sum; if (height[left] < height[right]) ++left; else --right; } returnmax; }