Skip to content

Commit 045a4e2

Browse files
committed
update
1 parent cef2b3d commit 045a4e2

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

CATEGORY.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,8 @@ https://garciparedes.me/#headers
9696
- [x] 35
9797
- [ ] 39
9898
- [ ] 40
99-
- [ ] 42
99+
- [x] 42
100100
- [ ] 45
101-
- [ ] 48
102-
- [ ] 53
103101
- [ ] 55
104102
- [ ] 56
105103
- [ ] 59
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @param {number[]} height
3+
* @return {number}
4+
*/
5+
var trap = function (height) {
6+
const leftMax = [0];
7+
const rightMax = [0];
8+
let res = 0;
9+
10+
for (let i = 1; i < height.length; i++) {
11+
leftMax[i] = Math.max(leftMax[i - 1], height[i - 1]);
12+
}
13+
14+
for (let j = height.length - 2; j >= 0; j--) {
15+
rightMax.unshift(Math.max(rightMax[0], height[j + 1]));
16+
}
17+
18+
for (let k = 1; k < height.length - 1; k++) {
19+
const temp = Math.min(leftMax[k], rightMax[k]) - height[k];
20+
if (temp > 0) {
21+
res += temp;
22+
}
23+
}
24+
25+
return res;
26+
};
27+
28+
console.log(trap([4, 2, 0, 3, 2, 5]));

0 commit comments

Comments
 (0)