-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11. Container With Most Water.cpp
48 lines (39 loc) · 1.03 KB
/
11. Container With Most Water.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class Solution {
public:
int maxArea(vector<int>& height) {
int n = height.size();
int i = 0 , j = n-1;
int area = 0;
int left = height[i];
int right = height[j];
while( i < j){
int val = min(height[i],height[j])*(j-i);
area = max(area,val);
if(height[i] <= height[j]) i++;
else{
j--;
}
}
return area;
}
};
class Solution {
public:
int maxArea(vector<int>& height) {
int area = 0 , max_area = 0;
int right = height.size() - 1 , left = 0 ;
while(left < right){
int length = right - left;
int breath = min(height[left],height[right]);
area = length*breath;
max_area = max(max_area,area);
if(height[left] < height[right] ){
left++;
}
else {
right--;
}
}
return max_area;
}
};