-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathContainerWithMostWater.java
172 lines (155 loc) · 4.39 KB
/
ContainerWithMostWater.java
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package LeetCodeJava.Greedy;
// https://leetcode.com/problems/container-with-most-water/
/**
* 11. Container With Most Water
* Solved
* Medium
* Topics
* Companies
* Hint
* You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]).
*
* Find two lines that together with the x-axis form a container, such that the container contains the most water.
*
* Return the maximum amount of water a container can store.
*
* Notice that you may not slant the container.
*
*
*
* Example 1:
*
*
* Input: height = [1,8,6,2,5,4,8,3,7]
* Output: 49
* Explanation: The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.
* Example 2:
*
* Input: height = [1,1]
* Output: 1
*
*
* Constraints:
*
* n == height.length
* 2 <= n <= 105
* 0 <= height[i] <= 104
*
*/
public class ContainerWithMostWater {
// V0
// IDEA : 2 POINTERS
/**
* IDEA: 2 POINTERS
*
* Step 0) init left, right pointer
* Step 1) get cur area, then move the `smaller val` pointer
* Step 2) repeat above steps, and maintain the `biggest` area
* Step 3) return the biggest area as result
*
*/
public int maxArea(int[] height) {
// edge
if (height == null || height.length == 0) {
return 0;
}
if (height.length == 1) {
return Math.abs(height[1] - height[0]);
}
// 2 pointers
int res = 0;
int l = 0;
int r = height.length - 1;
while (r > l) {
int h = Math.min(height[l], height[r]);
int w = r - l;
res = Math.max(res, h * w);
if (height[l] < height[r]) {
l += 1;
} else {
r -= 1;
}
}
return res;
}
// V0-1
// IDEA : 2 POINTERS
public int maxArea_0_1(int[] height) {
if (height.length == 0 || height.equals(null)){
return 0;
}
int ans = 0;
int left = 0;
// NOTE : right as height.length - 1
int right = height.length - 1;
// either ">=" or ">" is OK for this problem, but for logic alignment, we use ">=" here
while (right >= left){
int leftVal = height[left];
int rightVal = height[right];
// NOTE !!! right - left, we get distance between left, right pointer
int amount = (right - left) * Math.min(leftVal, rightVal);
ans = Math.max(amount, ans);
if (rightVal > leftVal){
left += 1;
}else{
right -= 1;
}
}
return ans;
}
// V0-2
// IDEA : BRUTE FORCE
public int maxArea_0_2(int[] height) {
if (height.length == 0 || height.equals(null)){
return 0;
}
int ans = 0;
for (int i = 0; i < height.length-1; i++){
for (int j = 1; j < height.length; j++){
int amount = (j - i) * Math.min(height[i], height[j]);
ans = Math.max(amount, ans);
}
}
return ans;
}
// V1
// IDEA : 2 POINTERS
public int maxArea_1(int[] height) {
if (height.length == 0 || height.equals(null)){
return 0;
}
int ans = 0;
int left = 0;
int right = height.length - 1;
while (right >= left){
int leftVal = height[left];
int rightVal = height[right];
int amount = (right - left) * Math.min(leftVal, rightVal);
ans = Math.max(amount, ans);
if (rightVal > leftVal){
left += 1;
}else{
right -= 1;
}
}
return ans;
}
// V2
// IDEA : 2 POINTERS
// https://leetcode.com/problems/container-with-most-water/editorial/
public int maxArea_2(int[] height) {
int maxarea = 0;
int left = 0;
int right = height.length - 1;
while (left < right) {
int width = right - left;
maxarea = Math.max(maxarea, Math.min(height[left], height[right]) * width);
if (height[left] <= height[right]) {
left++;
} else {
right--;
}
}
return maxarea;
}
}