(height)
| 7 | * |
| 8 | */ |
| 9 | var trap = function (height) { |
| 10 | const maxLeft = []; |
| 11 | const maxRight = []; |
| 12 | const minLeftRight = []; |
| 13 | |
| 14 | let current = 0; |
| 15 | for (let i = 0; i < height.length; i++) { |
| 16 | maxLeft.push(current); |
| 17 | current = Math.max(current, height[i]); |
| 18 | } |
| 19 | current = 0; |
| 20 | for (let i = height.length - 1; i > -1; i--) { |
| 21 | maxRight.push(current); |
| 22 | current = Math.max(current, height[i]); |
| 23 | } |
| 24 | // because the elements were added reverse. |
| 25 | maxRight.reverse(); |
| 26 | |
| 27 | for (let i = 0; i < height.length; i++) { |
| 28 | const minofLeftRight = Math.min(maxLeft[i], maxRight[i]); |
| 29 | minLeftRight.push(minofLeftRight); |
| 30 | } |
| 31 | |
| 32 | let water = 0; |
| 33 | for (let i = 0; i < height.length; i++) { |
| 34 | if (minLeftRight[i] - height[i] > 0) { |
| 35 | water += minLeftRight[i] - height[i]; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | return water; |
| 40 | }; |
| 41 | |
| 42 | /** |
| 43 | * https://leetcode.com/problems/trapping-rain-water/ |
nothing calls this directly
no test coverage detected