-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ11_ContainerWithMostWater.java
More file actions
55 lines (49 loc) · 1.75 KB
/
Q11_ContainerWithMostWater.java
File metadata and controls
55 lines (49 loc) · 1.75 KB
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
49
50
51
52
53
54
55
package com.so.leetcode;
/**
* 给定一个长度为 n 的整数数组 height 。有 n 条垂线,第 i 条线的两个端点是 (i, 0) 和 (i, height[i]) 。
* 找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。
* 返回容器可以储存的最大水量。
*
*
* @author FlyHippo
* @version 1.0
* @createDate 2024/5/11 9:39
* @tag 数组,双指针
* @link <a href="https://leetcode.cn/problems/container-with-most-water/description/">装最多水</a>
**/
public class Q11_ContainerWithMostWater {
/**
* 1. 左右指针分别在数组首尾
* 2. 计算当前面积,更新最大值
* 3. 移动较短边的指针(关键)
* 4. 重复直到指针相遇
*
* @param height
* @return
*/
public int maxArea(int[] height) {
// 初始化
int ans = 0, left = 0, right = height.length - 1;
// 当左指针小于右指针时,进循环
while (left < right) {
// 计算当前指针面积
int area = (right - left) * Math.min(height[left], height[right]);
ans = Math.max(ans, area);
// 矮边内移
if (height[left] < height[right]) {
++left;
} else {
--right;
}
System.out.println("left:"+left+" right:"+right+" area:"+area);
System.out.println("ans:"+ans);
System.out.println("===================");
}
return ans;
}
public static void main(String[] args) {
Q11_ContainerWithMostWater containerWithMostWater = new Q11_ContainerWithMostWater();
int[] ints = {1, 8, 6, 2, 5, 4, 8, 3, 7};
System.out.println(containerWithMostWater.maxArea(ints));
}
}