-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay12_LeetCode11.java
41 lines (38 loc) · 1.02 KB
/
Day12_LeetCode11.java
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
class Solution {
public int maxArea(int[] height) {
int i = 0;
int j = height.length-1;
int maxWater=0;
while(i<j){
int h = Math.min(height[i],height[j]);
int w = j-i;
int cWater = h*w;
maxWater = Math.max(cWater,maxWater);
if (height[i]>height[j]){
j--;
}else {
i++;
}
}
return maxWater;
}
}
//best solution
class Solution {
public int maxArea(int[] height) {
int left=0;
int right = height.length-1;
int max = Integer.MIN_VALUE;
while(left < right){
int h = Math.min(height[left], height[right]);
max = Math.max(max, h*(right - left));
while(left < right && height[left] <= h){
left++;
}
while(left < right && height[right] <= h){
right--;
}
}
return max;
}
}