Last active
March 21, 2018 03:58
-
-
Save sreeprasad/c6e4d400e8b3cbbc9760416916c3458c to your computer and use it in GitHub Desktop.
Container with most water area
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
class Solution { | |
public: | |
int maxArea(vector<int>& height) { | |
int N = height.size(); | |
int maxArea=0; | |
int left=0, right=N-1; | |
while (left<right) { | |
maxArea = max(maxArea, (min(height[left], height[right]) * (right-left)) ); | |
if (height[left] < height[right]) left++; | |
else right--; | |
} | |
return maxArea; | |
} | |
int max(int a, int b){ | |
return (a>=b) ? a : b; | |
} | |
int min(int a, int b){ | |
return a<=b ? a : b; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment