Created
April 22, 2013 05:44
-
-
Save chenha0/5432634 to your computer and use it in GitHub Desktop.
[LeetCode]Container With Most Water http://leetcode.com/onlinejudge#question_11
This file contains 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) { | |
// Start typing your C/C++ solution below | |
// DO NOT write int main() function | |
int area = (height.size() - 1) * ( (height.front() < height.back()) ? height.front() : height.back() ); | |
int i = 0, j = height.size() - 1; | |
while (i < j) { | |
int new_area = (j - i) * ( (height[i] < height[j]) ? height[i] : height[j] ); | |
if (new_area > area) area = new_area; | |
if (height[i] > height[j]) { | |
while (height[j] > height[--j]) {} | |
} else { | |
while (height[i] > height[++i]) {} | |
} | |
} | |
return area; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment