Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

complete trapping rain water #527

Merged
merged 2 commits into from
Oct 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions Dynamic-Programming/TrappingRainWater.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* @param {number[]} height
* @return {number}
*/

/* 42. Trapping Rain Water
https://leetcode.com/problems/trapping-rain-water/

Helpful animation of this prompt: https://youtu.be/HmBbcDiJapY?t=51

Given n non-negative integers representing an elevation map where
the width of each bar is 1, compute how much water it is able to trap
after raining.

VIEW ELEVATION MAP ON LEETCODE

Example:

Input: [0,1,0,2,1,0,1,3,2,1,2,1]
Output: 6

Plan:
iterate through and find left maxes
iterate through and find right maxes
create minheight and assign it to the min(leftmax, rightmax)
if current height(element) < minheight
push minheight - height into water array
else
push 0 onto water array

sum up water array and return

left maxes = [0,0,1,1,2,2,2,2,3,3,3,3]
right maxes = [3,3,3,3,3,3,3,2,2,2,1,0]
water contained = [0,0,1,0,1,2,1,0,0,1,0,0] -> sum = 6
*/

function trap (heights) {
const maxes = new Array(heights.length).fill(0)

let leftMax = 0
for (let i = 0; i < heights.length; i++) {
const height = heights[i]
maxes[i] = leftMax
leftMax = Math.max(leftMax, height)
}

let rightMax = 0
for (let i = heights.length - 1; i >= 0; i -= 1) {
const height = heights[i]
const minHeight = Math.min(rightMax, maxes[i])

if (height < minHeight) {
maxes[i] = minHeight - height
} else {
maxes[i] = 0
}
rightMax = Math.max(rightMax, height)
}
return maxes.reduce((a, b) => a + b, 0)
}

console.log(trap([0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1])) // -> 6
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@
"jest": "^26.4.2",
"standard": "^14.3.4"
}
}
}