Skip to content

Commit

Permalink
Merge branch 'jakwings-patch'
Browse files Browse the repository at this point in the history
  • Loading branch information
haoel committed Dec 30, 2014
2 parents a54003a + 0cccc8a commit 9804379
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/containerWithMostWater/containerWithMostWater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,26 @@ class Solution {

int area;
while ( left < right ){
//calculate the area
// calculate the area
area = (right - left) * ( height[left] < height[right] ? height[left] : height[right]);
//tracking the maxium area
// 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-- ;
//
// height[left] < height[right] ? left++ : right-- ;
//
// However, the above code could cause the unnecessary `area` cacluation
// We can do some improvement as below:
if (height[left] < height[right]) {
do {
left++;
} while (left < right && height[left-1] >= height[left]);
} else {
do {
right--;
} while (right > left && height[right+1] >= height[right]);
}
}

return maxArea;
Expand Down

0 comments on commit 9804379

Please sign in to comment.