Skip to content

Commit 33df3a1

Browse files
committed
add LeetCode 11. 盛最多水的容器
1 parent 0ea46b4 commit 33df3a1

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
![](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9jZG4uanNkZWxpdnIubmV0L2doL2Nob2NvbGF0ZTE5OTkvY2RuL2ltZy8yMDIwMDgyODE0NTUyMS5qcGc?x-oss-process=image/format,png)
2+
>仰望星空的人,不应该被嘲笑
3+
4+
## 题目描述
5+
给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0)。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。
6+
7+
**说明**:你不能倾斜容器,且 n 的值至少为 2。
8+
9+
![](https://img-blog.csdnimg.cn/20201008101004524.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjQyOTcxOA==,size_16,color_FFFFFF,t_70#pic_center)
10+
图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。
11+
12+
13+
14+
示例:
15+
16+
```javascript
17+
输入:[1,8,6,2,5,4,8,3,7]
18+
输出:49
19+
```
20+
来源:力扣(LeetCode)
21+
链接:https://leetcode-cn.com/problems/container-with-most-water
22+
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
23+
24+
25+
## 解题思路
26+
27+
双指针做法,我们需要枚举所有情况,有一点贪心的思想,每次我们得看短的板子让我们容纳的面积。每次都选择左右指针最短的那个板子,计算出当前容纳的最多的水,然后从短的板子指针出发向内缩,这样不断求,最终我们可以枚举所有情况,自然可以枚举出最大容器面积。
28+
29+
```javascript
30+
/**
31+
* @param {number[]} height
32+
* @return {number}
33+
*/
34+
var maxArea = function (height) {
35+
let len = height.length;
36+
let L = 0;
37+
let R = len - 1;
38+
let res = 0;
39+
while (L < R) {
40+
if (height[L] < height[R]) { // 选择短板效应
41+
let ans = height[L] * (R - L);
42+
L++;
43+
res = Math.max(res, ans); // 求当前容纳最多的水
44+
} else {
45+
let ans = height[R] * (R - L);
46+
res = Math.max(res, ans);
47+
R--;
48+
}
49+
}
50+
return res;
51+
};
52+
```
53+
54+
55+
56+
57+
## 最后
58+
文章产出不易,还望各位小伙伴们支持一波!
59+
60+
往期精选:
61+
62+
<a href="https://github.com/Chocolate1999/Front-end-learning-to-organize-notes">小狮子前端の笔记仓库</a>
63+
64+
<a href="https://github.com/Chocolate1999/leetcode-javascript">leetcode-javascript:LeetCode 力扣的 JavaScript 解题仓库,前端刷题路线(思维导图)</a>
65+
66+
小伙伴们可以在Issues中提交自己的解题代码,🤝 欢迎Contributing,可打卡刷题,Give a ⭐️ if this project helped you!
67+
68+
69+
<a href="https://yangchaoyi.vip/">访问超逸の博客</a>,方便小伙伴阅读玩耍~
70+
71+
![](https://img-blog.csdnimg.cn/2020090211491121.png#pic_center)
72+
73+
```javascript
74+
学如逆水行舟,不进则退
75+
```
76+
77+

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy