Hi, this is Shunchi!

  • Home

  • Tags0

  • Archives267

  • Categories0

  • Curricula

  • DSA

  • LeetCode_Notes

  • Interviews

  • General

  • Resume

11. Container With Most Water

Posted on 2020-06-13 | Edited on 2021-01-22

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;
}
}

<1…161162163…267>
ShunchiZhou

ShunchiZhou

267 posts
RSS
GitHub E-Mail Gitbook Linkedin
© 2024 ShunchiZhou
Powered by Hexo v5.4.0
|
0%