-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11.盛最多水的容器.java
40 lines (35 loc) · 1.07 KB
/
11.盛最多水的容器.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
/*
* @lc app=leetcode.cn id=11 lang=java
*
* [11] 盛最多水的容器
*/
// @lc code=start
class Solution {
public int maxArea(int[] height) {
return maxAreaTwoPointers(height);
}
int maxAreaTwoPointers(int[] height) {
// from leetcode official
// when height[left] < height[right], all candidate right borders at left side of "right" won't be in solution
// space, so the "left" should step right.
int maxVolume = 0;
int left = 0;
int right = height.length - 1;
while (left < right) {
int heightOfWater = 0;
int widthOfWater = 0;
if (height[left] < height[right]) {
heightOfWater = height[left];
widthOfWater = right - left;
left++;
} else {
heightOfWater = height[right];
widthOfWater = right - left;
right--;
}
maxVolume = Math.max(maxVolume, widthOfWater * heightOfWater);
}
return maxVolume;
}
}
// @lc code=end