Skip to content

Instantly share code, notes, and snippets.

@revanth0212
Created June 23, 2018 16:09
Show Gist options
  • Save revanth0212/1c6971e50ebe40b15da97c0a0608b66b to your computer and use it in GitHub Desktop.
Save revanth0212/1c6971e50ebe40b15da97c0a0608b66b to your computer and use it in GitHub Desktop.
Find the number of ways to climb N number of stairs. (DP)
const hashMap = {}
function countNumberOfWays (steps) {
if (steps <= 0) {
return 0
} else if (steps === 1) {
return 1
} else if (steps === 2) {
return 2
} else {
if (hashMap.hasOwnProperty(steps)) {
return hashMap[steps]
} else {
const val = countNumberOfWays(steps - 1) + countNumberOfWays(steps - 2)
hashMap[steps] = val
return val
}
}
}
console.log(countNumberOfWays(4))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment