Add comments for "Container With Most Water"
This commit is contained in:
parent
990d56f06c
commit
f610c45a4a
@ -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;
|
||||
}
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user