Created
September 17, 2012 14:20
-
-
Save hyfrey/3737593 to your computer and use it in GitHub Desktop.
leetcode: 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
| class Solution { | |
| public: | |
| int max(int a, int b) { | |
| return a > b ? a : b; | |
| } | |
| int min(int a, int b) { | |
| return a < b ? a : b; | |
| } | |
| int maxArea(vector<int> &height) { | |
| int i = 0; | |
| int j = height.size() - 1; | |
| int max_area = 0; | |
| while (i < j) { | |
| int area = min(height[i], height[j]) * (j - i); | |
| max_area = max(max_area, area); | |
| if (height[i] < height[j]) { | |
| i++; | |
| } else { | |
| j--; | |
| } | |
| } | |
| return max_area; | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment