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+ // Time Complexity: O(n), n: height.length
2+ // Space Complexity: O(1)
3+ class Solution {
4+ public int maxArea (int [] height ) {
5+ int maxWaterAmount = 0 ;
6+ int leftLineIdx = 0 ;
7+ int rightLineIdx = height .length -1 ;
8+
9+ while (leftLineIdx < rightLineIdx ) {
10+ int leftHeight = height [leftLineIdx ];
11+ int rightHeight = height [rightLineIdx ];
12+ int tempAmount = 0 ;
13+
14+ if (leftHeight < rightHeight ) {
15+ tempAmount = leftHeight * (rightLineIdx - leftLineIdx );
16+ leftLineIdx ++;
17+ } else {
18+ tempAmount = rightHeight * (rightLineIdx - leftLineIdx );
19+ rightLineIdx --;
20+ }
21+
22+ // update maximum amount
23+ maxWaterAmount = tempAmount > maxWaterAmount ? tempAmount : maxWaterAmount ;
24+ }
25+
26+ return maxWaterAmount ;
27+ }
28+ }
You can’t perform that action at this time.
0 commit comments