Skip to content

Instantly share code, notes, and snippets.

@sreeprasad
Last active March 21, 2018 03:58
Show Gist options
  • Save sreeprasad/c6e4d400e8b3cbbc9760416916c3458c to your computer and use it in GitHub Desktop.
Save sreeprasad/c6e4d400e8b3cbbc9760416916c3458c to your computer and use it in GitHub Desktop.
Container with most water area
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