Skip to content

Instantly share code, notes, and snippets.

@walkingtospace
Created April 5, 2015 07:02
Show Gist options
  • Save walkingtospace/16d0ebd52929374411c5 to your computer and use it in GitHub Desktop.
Save walkingtospace/16d0ebd52929374411c5 to your computer and use it in GitHub Desktop.
container-with-most-water
/*
https://leetcode.com/problems/container-with-most-water/
Did I clearly understand the problem?
Is the given input is sorted? No
| |
| | | |
| | | | | | |
|------------------
MAX = abs(x_1, x_1) * min(y_2, y_1)
Simple solution : exhaustive comparison O(N^2)
*/
class Solution {
public:
int maxArea(vector<int> &height) {
int left=0, right=height.size()-1, maxArea = 0;
while(left < right) {
maxArea = max(maxArea, abs(left-right)*min(height[left], height[right]));
if(height[left] < height[right]) left++;
else right--;
}
return maxArea;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment