-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxWater.js
More file actions
31 lines (27 loc) · 798 Bytes
/
maxWater.js
File metadata and controls
31 lines (27 loc) · 798 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//start with left and right pointers.
//calculate the water between them, if it greater than maxWater, then assign maxWater to that value
//if left is greater than right, decrease right
//if right is greater than or equal to left, increase left
//
/**
* @param {number[]} height
* @return {number}
*/
var maxArea = function (height) {
let left = 0;
let right = height.length - 1;
let maxWater = 0;
while (left < right) {
if (height[left] > height[right]) {
let currWater = (right - left) * height[right]
if (maxWater < currWater) maxWater = currWater;
right--;
}
else {
let currWater = (right - left) * height[left]
if (maxWater < currWater) maxWater = currWater;
left++;
}
}
return maxWater;
};