Skip to content

Commit 5da333d

Browse files
committed
solve 11.盛最多水的容器
1 parent 5fa3f60 commit 5da333d

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

cn/11.盛最多水的容器.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* @lc app=leetcode.cn id=11 lang=java
3+
*
4+
* [11] 盛最多水的容器
5+
*
6+
* https://leetcode-cn.com/problems/container-with-most-water/description/
7+
*
8+
* algorithms
9+
* Medium (63.24%)
10+
* Likes: 1439
11+
* Dislikes: 0
12+
* Total Accepted: 207.1K
13+
* Total Submissions: 327.5K
14+
* Testcase Example: '[1,8,6,2,5,4,8,3,7]'
15+
*
16+
* 给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为
17+
* (i, ai) 和 (i, 0)。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。
18+
*
19+
* 说明:你不能倾斜容器,且 n 的值至少为 2。
20+
*
21+
*
22+
*
23+
*
24+
*
25+
* 图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。
26+
*
27+
*
28+
*
29+
* 示例:
30+
*
31+
* 输入:[1,8,6,2,5,4,8,3,7]
32+
* 输出:49
33+
*
34+
*/
35+
36+
// @lc code=start
37+
class Solution {
38+
public int maxArea(int[] height) {
39+
int max = 0;
40+
int left = 0;
41+
int right = height.length - 1;
42+
43+
while (left < right) {
44+
max = Math.max(max, Math.min(height[left], height[right]) * (right - left));
45+
if (height[left] <= height[right]) {
46+
left++;
47+
} else {
48+
right--;
49+
}
50+
}
51+
52+
return max;
53+
}
54+
}
55+
// @lc code=end
56+

0 commit comments

Comments
 (0)