11. Container With Most Water

LeetCode

Approach 1: Brute Force, Time Complexity: O(n^2)

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public int maxArea(int[] height) {
int max=0;
for(int i=0;i<height.length-1;i++){
for(int j=i+1;j<height.length;j++){
max=Math.max((j-i)*Math.min(height[i],height[j]),max);
}
}
return max;
}
}

Approach 2: Two Pointer Approach

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public int maxArea(int[] height) {
int l=0,r=height.length-1;
int max=0;
while(l<r){
max=Math.max(max,(r-l)*Math.min(height[l],height[r]));
if(height[l]<height[r]) l++;
else r--;
}
return max;
}
}

0%