Skip to content

Commit 4128fed

Browse files
committed
feat: container-with-most-water
1 parent a0cdd4d commit 4128fed

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/container-with-most-water/">week06-2.container-with-most-water</a>
3+
* <li>Description: Return the maximum amount of water a container can store</li>
4+
* <li>Topics: Array, Two Pointers, Greedy </li>
5+
* <li>Time Complexity: O(N), Runtime 5ms </li>
6+
* <li>Space Complexity: O(1), Memory 57.42MB </li>
7+
*/
8+
class Solution {
9+
public int maxArea(int[] height) {
10+
int maxArea = 0;
11+
int left = 0;
12+
int right = height.length - 1;
13+
14+
while (left < right) {
15+
int width = right - left;
16+
int minHeight = Math.min(height[left], height[right]);
17+
maxArea = Math.max(maxArea, width * minHeight);
18+
19+
if (height[left] < height[right]) {
20+
left++;
21+
} else {
22+
right--;
23+
}
24+
}
25+
26+
return maxArea;
27+
}
28+
}

0 commit comments

Comments
 (0)