-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0011-ContainerWithMostWater.js
43 lines (36 loc) · 1.13 KB
/
0011-ContainerWithMostWater.js
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
32
33
34
35
36
37
38
39
40
41
42
43
//-----------------------------------------------------------------------------
// Runtime: 80ms
// Memory Usage: 38.1 MB
// Link: https://leetcode.com/submissions/detail/385805690/
//-----------------------------------------------------------------------------
var solution = function() {
'use strict';
/**
* @param {number[]} height
* @return {number}
*/
var maxArea = function(height) {
let left = 0, right = height.length - 1;
let result = 0;
while (left < right) {
let area = Math.min(height[left], height[right]) * (right - left);
result = Math.max(result, area);
if (height[left] <= height[right]) {
let temp = height[left];
do {
left++;
} while(left < right && height[left] <= temp);
} else {
let temp = height[right];
do {
right--;
} while(left < right && height[right] <= temp);
}
}
return result;
};
return {
maxArea: maxArea
};
};
module.exports = solution();