Skip to content

Commit 3a537d5

Browse files
committed
Added container with most water solution
1 parent 3e99718 commit 3a537d5

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

container-with-most-water/nhistory.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var maxArea = function (height) {
2+
// Two pointer: left and right
3+
// Amount of water: math.Min(height[left], height[right]) * (right-left)
4+
5+
// Eception case
6+
if (height.length === 0) return 0;
7+
8+
let left = 0;
9+
let right = height.length - 1;
10+
let result = 0;
11+
12+
// Iterate to find maxiume amount of water
13+
while (left < right) {
14+
const amount = Math.min(height[left], height[right]) * (right - left);
15+
result = Math.max(result, amount);
16+
height[left] <= height[right] ? left++ : right--;
17+
}
18+
19+
return result;
20+
};
21+
22+
// TC: O(n)
23+
// SC: O(1)

0 commit comments

Comments
 (0)