Skip to content

Commit

Permalink
Add comments for "Container With Most Water"
Browse files Browse the repository at this point in the history
  • Loading branch information
haoel committed Oct 22, 2014
1 parent 990d56f commit f610c45
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/containerWithMostWater/containerWithMostWater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,23 @@
class Solution {
public:
int maxArea(vector<int> &height) {
int area = 0;
int i = 0;
int j = height.size()-1;

while(i<j){
int a = (j-i)* (height[i]>height[j] ? height[j] : height[i]);
area = a>area ? a : area;
height[i]>height[j] ? j-- : i++;

int maxArea = 0;
// two pointers scan from two sides to middle
int left = 0;
int right = height.size()-1;

int area;
while ( left < right ){
//calculate the area
area = (right - left) * ( height[left] < height[right] ? height[left] : height[right]);
//tracking the maxium area
maxArea = area > maxArea ? area : maxArea;
// because the area is decided by the shorter edge
// so we increase the area is to increase the shorter edge
height[left] < height[right] ? left++ : right-- ;
}

return area;
return maxArea;
}
};

0 comments on commit f610c45

Please sign in to comment.