-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMostWater.java
More file actions
36 lines (28 loc) · 1.13 KB
/
Copy pathMostWater.java
File metadata and controls
36 lines (28 loc) · 1.13 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
/*
11. 盛最多水的容器 (https://leetcode-cn.com/problems/container-with-most-water)
给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0)。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。
说明:你不能倾斜容器,且 n 的值至少为 2。
图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。
示例:
输入: [1,8,6,2,5,4,8,3,7]
输出: 49
*/
package twopointers;
public class MostWater {
public static int maxArea(int[] height) {
int i = 0, j = height.length - 1;
int max = Integer.MIN_VALUE;
while (i < j) {
int area = Math.min(height[i], height[j]) * (j - i);
max = Math.max(max, area);
if (height[i] < height[j]) {
}
if (height[i] < height[j]) {
i++;
} else {
j--;
}
}
return max;
}
}