File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed
container-with-most-water Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments