From df571c3c50032498a86b857ff085837e89f3b0c4 Mon Sep 17 00:00:00 2001 From: aadil42 <77232799+aadil42@users.noreply.github.com> Date: Tue, 15 Aug 2023 18:45:45 +0530 Subject: [PATCH] Create code.js Adding solution to the min-cost-climbing-stairs in js. Submission Link: https://leetcode.com/problems/min-cost-climbing-stairs/submissions/1022073402/ --- .../009. Min Cost Climbing Stairs/code.js | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 02. Algorithms/09. Dynamic Programming/009. Min Cost Climbing Stairs/code.js diff --git a/02. Algorithms/09. Dynamic Programming/009. Min Cost Climbing Stairs/code.js b/02. Algorithms/09. Dynamic Programming/009. Min Cost Climbing Stairs/code.js new file mode 100644 index 00000000..76c03af4 --- /dev/null +++ b/02. Algorithms/09. Dynamic Programming/009. Min Cost Climbing Stairs/code.js @@ -0,0 +1,24 @@ +/** + * Recursion + * Time O(n) | Space O(n) + * @param {number[]} cost + * @return {number} + */ +var minCostClimbingStairs = function(cost) { + + const memo = { + [cost.length - 1]: cost[cost.length - 1] + }; + function dfs(index) { + if(memo[index]) return memo[index]; + if(index >= cost.length) return 0; + const choice1 = dfs(index+1); + const choice2 = dfs(index+2); + memo[index] = cost[index] + Math.min(choice1, choice2); + return memo[index]; + } + // console.log(memo); + // console.log(memo); + return Math.min(dfs(0), dfs(1)); + // return dfs(0); +};