-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathContainerWithMostWater.java
More file actions
127 lines (117 loc) · 4.08 KB
/
Copy pathContainerWithMostWater.java
File metadata and controls
127 lines (117 loc) · 4.08 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* <p>给你 <code>n</code> 个非负整数 <code>a<sub>1</sub>,a<sub>2,</sub>...,a</code><sub><code>n</code>,</sub>每个数代表坐标中的一个点 <code>(i, a<sub>i</sub>)</code> 。在坐标内画 <code>n</code> 条垂直线,垂直线 <code>i</code> 的两个端点分别为 <code>(i, a<sub>i</sub>)</code> 和 <code>(i, 0)</code> 。找出其中的两条线,使得它们与 <code>x</code> 轴共同构成的容器可以容纳最多的水。</p>
*
* <p><strong>说明:</strong>你不能倾斜容器。</p>
*
* <p> </p>
*
* <p><strong>示例 1:</strong></p>
*
* <p><img alt="" src="https://aliyun-lc-upload.oss-cn-hangzhou.aliyuncs.com/aliyun-lc-upload/uploads/2018/07/25/question_11.jpg" style="height: 287px; width: 600px;" /></p>
*
* <pre>
* <strong>输入:</strong>[1,8,6,2,5,4,8,3,7]
* <strong>输出:</strong>49
* <strong>解释:</strong>图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。</pre>
*
* <p><strong>示例 2:</strong></p>
*
* <pre>
* <strong>输入:</strong>height = [1,1]
* <strong>输出:</strong>1
* </pre>
*
* <p><strong>示例 3:</strong></p>
*
* <pre>
* <strong>输入:</strong>height = [4,3,2,1,4]
* <strong>输出:</strong>16
* </pre>
*
* <p><strong>示例 4:</strong></p>
*
* <pre>
* <strong>输入:</strong>height = [1,2,1]
* <strong>输出:</strong>2
* </pre>
*
* <p> </p>
*
* <p><strong>提示:</strong></p>
*
* <ul>
* <li><code>n == height.length</code></li>
* <li><code>2 <= n <= 10<sup>5</sup></code></li>
* <li><code>0 <= height[i] <= 10<sup>4</sup></code></li>
* </ul>
* <div><div>Related Topics</div><div><li>贪心</li><li>数组</li><li>双指针</li></div></div><br><div><li>👍 3076</li><li>👎 0</li></div>
*/
package leetcode1;
public class ContainerWithMostWater {
public static void main(String[] args) {
Solution solution = new ContainerWithMostWater().new Solution();
}
class Solution {
public int maxArea(int[] height) {
int max = 0;
for (int left = 0, right = height.length - 1; left < right; ) {
int h = Math.min(height[left], height[right]);
max = Math.max((right - left) * h, max);
if (height[left] < height[right]) left++;
else right--;
}
return max;
}
}
/**
* 左右夹逼 O(n),易理解的解法
* 双指针法,易理解的证明:
* https://leetcode-cn.com/problems/container-with-most-water/solution/on-shuang-zhi-zhen-jie-fa-li-jie-zheng-que-xing-tu/
*/
class Solution1 {
public int maxArea(int[] height) {
int i = 0;
int j = height.length - 1;
int maxArea = 0;
while (i < j) {
if (height[i] < height[j]) {
maxArea = Math.max(maxArea, height[i] * (j - i));
i++;
} else {
maxArea = Math.max(maxArea, height[j] * (j - i));
j--;
}
}
return maxArea;
}
}
/**
* 左右夹逼 O(n),极致优化写法
*/
class Solution2 {
public int maxArea(int[] height) {
int area = 0;
for (int i = 0, j = height.length - 1; i < j; ) {
int h = (height[i] < height[j]) ? height[i++] : height[j--];
int newArea = h * (j - i + 1);
area = Math.max(newArea, area);
}
return area;
}
}
/**
* 暴力遍历 O(n2)
*/
class Solution3 {
public int maxArea(int[] height) {
int maxArea = 0;
for (int i = 0; i < height.length; i++) {
for (int j = i + 1; j < height.length; j++) {
int area = Math.min(height[j], height[i]) * (j - i);
maxArea = Math.max(maxArea, area);
}
}
return maxArea;
}
}
}