Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1b031f7

Browse filesBrowse files
committed
feat: add 011
1 parent 31281be commit 1b031f7
Copy full SHA for 1b031f7

File tree

Expand file treeCollapse file tree

4 files changed

+51
-2
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+51
-2
lines changed

‎README.md

Copy file name to clipboardExpand all lines: README.md
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
| 5 | [Longest Palindromic Substring][005] | String |
7373
| 6 | [ZigZag Conversion][006] | String |
7474
| 8 | [String to Integer (atoi)][008] | Math, String |
75+
| 11 | [Container With Most Water][011] | Array, Two Pointers |
7576
| 15 | [3Sum][015] | Array, Two Pointers |
7677
| 17 | [Letter Combinations of a Phone Number][017] | String, Backtracking |
7778
| 19 | [Remove Nth Node From End of List][019] | Linked List, Two Pointers |
@@ -141,6 +142,7 @@
141142
[005]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/005/README.md
142143
[006]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/006/README.md
143144
[008]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/008/README.md
145+
[011]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/011/README.md
144146
[015]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/015/README.md
145147
[017]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/017/README.md
146148
[019]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/019/README.md

‎note/011/README.md

Copy file name to clipboardExpand all lines: note/011/README.md
+20-2Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,30 @@ Note: You may not slant the container and *n* is at least 2.
1111

1212
## 思路
1313

14+
题意是给你 *a1*, *a2*, ..., *an**n* 个数,代表 (*i*, *ai*) 坐标,让你从中找两个点与 x 轴围成的容器可以容纳最多的水。
1415

16+
不明白的话可以看数据为 `1 8 6 2 5 4 8 3 7` 所示的图。
1517

16-
```java
18+
![](https://raw.githubusercontent.com/Blankj/awesome-java-leetcode/master/note/011/water.png)
19+
20+
如果用暴力法求每种情况的结果,其时间复杂度为 O(n^2),相信肯定会超时,我们可以探索下是否有更巧妙的办法呢,题目的标签有双指针,是否就可以想到首尾各放一指针,然后根据条件来收缩。首先计算一次首尾构成的最大面积,然后分析下该移动哪个指针,如果移动大的那个指针的话,那样只会减小面积,所以我们要移动小的那个指针,小的那个指针移动到哪呢?当然是移动到大于之前的值的地方,否则面积不都比之前小么,然后继续更新最大值即可,借助如上分析写出如下代码应该不是什么难事了吧。
1721

18-
```
1922

23+
```java
24+
class Solution {
25+
public int maxArea(int[] height) {
26+
int l = 0, r = height.length - 1;
27+
int max = 0, h = 0;
28+
while (l < r) {
29+
h = Math.min(height[l], height[r]);
30+
max = Math.max(max, (r - l) * h);
31+
while (height[l] <= h && l < r) ++l;
32+
while (height[r] <= h && l < r) --r;
33+
}
34+
return max;
35+
}
36+
}
37+
```
2038

2139

2240
## 结语

‎note/011/water.png

Copy file name to clipboard
41.2 KB
Loading
+29Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.blankj.medium._011;
2+
3+
/**
4+
* <pre>
5+
* author: Blankj
6+
* blog : http://blankj.com
7+
* time : 2017/04/23
8+
* desc :
9+
* </pre>
10+
*/
11+
public class Solution {
12+
public int maxArea(int[] height) {
13+
int l = 0, r = height.length - 1;
14+
int max = 0, h = 0;
15+
while (l < r) {
16+
h = Math.min(height[l], height[r]);
17+
max = Math.max(max, (r - l) * h);
18+
while (height[l] <= h && l < r) ++l;
19+
while (height[r] <= h && l < r) --r;
20+
}
21+
return max;
22+
}
23+
24+
public static void main(String[] args) {
25+
Solution solution = new Solution();
26+
System.out.println(solution.maxArea(new int[]{1, 2, 4, 3})); // 4
27+
System.out.println(solution.maxArea(new int[]{1, 8, 6, 2, 5, 4, 8, 3, 7}));// 49
28+
}
29+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.