-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11.盛最多水的容器.js
48 lines (47 loc) · 1.13 KB
/
11.盛最多水的容器.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
44
45
46
47
48
/**
* @param {number[]} height
* @return {number}
*/
/*
* 题解1: 枚举
* 时间复杂度 On2
*/
var maxArea = function (height) {
const len = height.length
let max = 0
for (let i = 0; i < len; i++) {
for (let j = i + 1; j < len; j++) {
var area = (j - i) * Math.min(height[i], height[j])
max = Math.max(area, max)
}
}
return max
}
/*
* 题解2:双指针,从两边开始往里找,记录最大面积;哪边高则哪边指针保持不动,另外一个指针移动
* 时间复杂度On
*/
var maxArea = function(height) {
let L = 0
let R = height.length - 1
let res = 0
let sum = 0
while (L < R) {
sum = Math.min(height[L], height[R]) * (R - L)
if (height[L] < height[R]) {
L++
} else {
R--
}
if (sum > res) res = sum
}
return res
};
var maxArea = function (height) {
let max = 0
for (let i = 0, j = height.length - 1; i < j;) {
let minHeight = height[i] < height[j] ? height[i++] : height[j--]
max = Math.max(minHeight * (j - i), max)
}
return max
}