Created
April 5, 2015 07:02
-
-
Save walkingtospace/16d0ebd52929374411c5 to your computer and use it in GitHub Desktop.
container-with-most-water
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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