Skip to content

Commit df3de4d

Browse files
committed
Day90
1 parent 24dcdc5 commit df3de4d

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

Day90/index.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* @param {number[]} height
3+
* @return {number}
4+
*/
5+
var trap = function(height) {
6+
// [0,0,1,1,2,2,2,2,3,3,3,3]
7+
8+
// [3 3 3 3 3 3 3 2 2 2 1,0]
9+
10+
/*
11+
[0,1,0,2,1,0,1,3,2,1,2,1]
12+
13+
[0,0,1,1,2,2,2,2,3,3,3,3]
14+
[3,3,3,3,3,3,3,2,2,2,1,0]
15+
16+
*/
17+
18+
if(!height.length)return 0
19+
let leftData = [];
20+
let left = 0;
21+
22+
for(let i=0; i < height.length; i++){
23+
leftData[i] = left;
24+
left = Math.max(left,height[i] )
25+
}
26+
27+
28+
29+
let rightData = [];
30+
let right = 0;
31+
32+
for(let i=height.length-1; i>=0; i--){
33+
rightData[i] = right;
34+
right = Math.max(right,height[i] )
35+
}
36+
37+
let ans = [];
38+
39+
for(let i =0; i < height.length; i++){
40+
let d = Math.min(rightData[i], leftData[i]);
41+
42+
if(height[i] < d){
43+
ans.push(d-height[i])
44+
}
45+
}
46+
console.log(ans)
47+
48+
return ans.reduce((a,b)=> a+b,0)
49+
};

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,6 @@ If you are loving solving problems in leetcode, please contact me to enjoy it to
198198

199199
|Day 88| [526. Beautiful Arrangement](https://leetcode.com/problems/beautiful-arrangement/) | [javascript]()|[:memo:](https://leetcode.com/problems/beautiful-arrangement/)|Medium|
200200

201-
|Day 89| [17. Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/) | [javascript]()|[:memo:](https://leetcode.com/problems/letter-combinations-of-a-phone-number/)|Medium|
201+
|Day 89| [17. Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/) | [javascript]()|[:memo:](https://leetcode.com/problems/letter-combinations-of-a-phone-number/)|Medium|
202+
203+
|Day 90| [42. Trapping Rain Water](https://leetcode.com/problems/trapping-rain-water/) | [javascript]()|[:memo:](https://leetcode.com/problems/trapping-rain-water/)|Hard|

0 commit comments

Comments
 (0)