Skip to content

Commit 8ad2f39

Browse files
committed
add solution of container-with-most-water
1 parent 380b1ae commit 8ad2f39

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
3+
/**
4+
* time-complexity : O(n)
5+
* space-complexity : O(1)
6+
*/
7+
8+
public int maxArea(int[] height) {
9+
10+
int left = 0;
11+
int right = height.length - 1;
12+
13+
int w = 0, h = 0, currSize = 0, maxSize = 0;
14+
15+
while (left < right) {
16+
17+
w = right - left;
18+
h = Math.min(height[left], height[right]);
19+
20+
currSize = w * h;
21+
maxSize = Math.max(currSize, maxSize);
22+
23+
if (height[left] < height[right]) {
24+
left++;
25+
} else {
26+
right--;
27+
}
28+
29+
}
30+
return maxSize;
31+
}
32+
}

0 commit comments

Comments
 (0)