Created
June 23, 2018 16:09
-
-
Save revanth0212/1c6971e50ebe40b15da97c0a0608b66b to your computer and use it in GitHub Desktop.
Find the number of ways to climb N number of stairs. (DP)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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