Skip to content

Instantly share code, notes, and snippets.

@chenha0
Created April 22, 2013 05:44
Show Gist options
  • Save chenha0/5432634 to your computer and use it in GitHub Desktop.
Save chenha0/5432634 to your computer and use it in GitHub Desktop.
[LeetCode]Container With Most Water http://leetcode.com/onlinejudge#question_11
class Solution {
public:
int maxArea(vector<int> &height) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int area = (height.size() - 1) * ( (height.front() < height.back()) ? height.front() : height.back() );
int i = 0, j = height.size() - 1;
while (i < j) {
int new_area = (j - i) * ( (height[i] < height[j]) ? height[i] : height[j] );
if (new_area > area) area = new_area;
if (height[i] > height[j]) {
while (height[j] > height[--j]) {}
} else {
while (height[i] > height[++i]) {}
}
}
return area;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment