-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path42.接雨水.js
More file actions
71 lines (66 loc) · 1.54 KB
/
42.接雨水.js
File metadata and controls
71 lines (66 loc) · 1.54 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。
//
//
//
// 示例 1:
//
//
//
//
//输入:height = [0,1,0,2,1,0,1,3,2,1,2,1]
//输出:6
//解释:上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。
//
//
// 示例 2:
//
//
//输入:height = [4,2,0,3,2,5]
//输出:9
//
//
//
//
// 提示:
//
//
// n == height.length
// 1 <= n <= 2 * 10⁴
// 0 <= height[i] <= 10⁵
//
// Related Topics 栈 数组 双指针 动态规划 单调栈 👍 3191 👎 0
//leetcode submit region begin(Prohibit modification and deletion)
/**
* @param {number[]} height
* @return {number}
*/
// 左右最大值解法
// var trap = function(height) {
// if(height.length < 3) {
// return 0
// }
//
// const len = height.length
//
// const leftMaxArray = new Array(len).fill(0)
// const rightMaxArray = new Array(len).fill(0)
// leftMaxArray[0] = height[0]
// rightMaxArray[len - 1] = height[len - 1]
//
// for(let i = 1; i < len; i++) {
// leftMaxArray[i] = Math.max(leftMaxArray[i - 1], height[i])
// }
//
// for(let i = len - 2; i >= 0; i--) {
// rightMaxArray[i] = Math.max(rightMaxArray[i + 1], height[i])
// }
//
// let result = 0
//
// for(let i = 0; i < len; i++) {
// result += Math.min(leftMaxArray[i], rightMaxArray[i]) - height[i]
// }
//
// return result
// };
//leetcode submit region end(Prohibit modification and deletion)