We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent c44a622 commit 347110aCopy full SHA for 347110a
42-trapping-rain-water.js
@@ -21,3 +21,26 @@ const trap = function(height) {
21
}
22
return res
23
};
24
+
25
+// another
26
27
+/**
28
+ * @param {number[]} height
29
+ * @return {number}
30
+ */
31
+const trap = function(height) {
32
+ const len = height.length
33
+ if (len === 0) return 0
34
+ const leftMax = [height[0]]
35
+ const rightMax = []
36
+ rightMax[len - 1] = height[len - 1]
37
+ for (let i = len - 2; i >= 0; i--) {
38
+ rightMax[i] = Math.max(height[i], rightMax[i + 1])
39
+ }
40
+ let res = 0
41
+ for (let i = 1; i < len; i++) {
42
+ leftMax[i] = Math.max(height[i], leftMax[i - 1])
43
+ res += Math.min(leftMax[i], rightMax[i]) - height[i]
44
45
+ return res
46
+}
0 commit comments