-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay 070-Container With Most Water
48 lines (42 loc) · 1.06 KB
/
Day 070-Container With Most Water
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
// https://leetcode.com/problems/container-with-most-water/
class Solution {
public int maxArea(int[] height) {
int len = height.length;
int res = 0;
int left = 0 ;
int right = len-1;
int area = 0;
while(left < right){
area = (right - left) * Math.min(height[left] , height[right]);
res = Math.max(res , area);
if(height[left] < height[right]){
left += 1;
}
else {
right -= 1;
}
}
return res;
}
}
c++ code
class Solution {
public:
int maxArea(vector<int>& height) {
int max = INT_MIN;
int l =0 , r = height.size()-1;
while(l < r){
int a = ((r+1)-(l+1)) * min(height.at(l) , height.at(r));
// cout<<a<<"\n";
if(a > max){
max =a;
}
if(height.at(l) >= height.at(r)){
r--;
}
else
l++;
}
return max;
}
};