|
| 1 | +//////////////////////////////////////////////////////////////// |
| 2 | +// LAST step's price !== 0 |
| 3 | +//////////////////////////////////////////////////////////////// |
| 4 | + |
| 5 | +//////////////////////////////////////////////////////////////// |
| 6 | +// Framework for solving DP Problems |
| 7 | +// 1. Define the objective function |
| 8 | +//// f(n) is the cheapest way to get to the last step |
| 9 | +// 2. Identify base cases, |
| 10 | +////([3, 2, 4, 6, 1, 1, 5, 3]); // => 0--3--2--4--6--1--1--5--3 |
| 11 | +//// f(0) = 0 |
| 12 | +//// f(1) = 3 |
| 13 | +//// f(2) = 2 |
| 14 | +//// f(3) = 6 |
| 15 | +// 3. Write down: Recurrence Relation for the optimized objective function |
| 16 | +//// f(n) = n + min(fn-1, fn-2) |
| 17 | + |
| 18 | +// 4. What's the order of execution? |
| 19 | +//// bottom-up |
| 20 | +// 5. Where to look for the answer? |
| 21 | +//// f(n) |
| 22 | +//////////////////////////////////////////////////////////////// CHEAPEST PATH |
| 23 | +// // n steps, k steps, prices |
| 24 | +// const minCostStairs = (cost) => { |
| 25 | +// const path = new Set(); |
| 26 | +// const costPerStep = [0, cost[0], cost[1]]; |
| 27 | + |
| 28 | +// for (let i = 2; i < cost.length; i++) { |
| 29 | +// cost[i] = cost[i] + Math.min(cost[i - 1], cost[i - 2]); |
| 30 | +// Math.min(cost[i - 1], cost[i - 2]) === cost[i - 1] |
| 31 | +// ? steps.add(i - 1) |
| 32 | +// : steps.add(i - 2); |
| 33 | +// } |
| 34 | +// steps.add(cost[cost.length - 1]); |
| 35 | +// console.log(steps); |
| 36 | +// }; |
| 37 | +// // n = 8 stairs, k = 2 steps, |
| 38 | +// minCostStairs([3, 2, 4, 6, 1, 1, 5, 3]); // => 0--3--2--4--6--1--1--5--3 |
| 39 | + |
| 40 | +// ABOVE |
| 41 | +// OPTIMIZED TO THIS: time O(n), space O(1) |
| 42 | +// UNDER |
| 43 | +//////////////////////////////////////////////////////////////// CHEAPEST PATH WITH OPTIMIZED COST |
| 44 | + |
| 45 | +// // n steps, k steps, prices |
| 46 | +const minCostStairs = (cost) => { |
| 47 | + // const price = [0, ...cost]; |
| 48 | + const path = new Set(); |
| 49 | + let count = 0; |
| 50 | + |
| 51 | + for (let i = 2; i < cost.length; i++) { |
| 52 | + cost[i] = cost[i] + Math.min(cost[i - 1], cost[i - 2]); |
| 53 | + // cost[i - 1] < cost[i - 2] ? (count += cost[i - 1]) : (count += cost[i - 2]); |
| 54 | + cost[i - 1] < cost[i - 2] && path.add(i - 1); // not add when === |
| 55 | + cost[i - 1] > cost[i - 2] && path.add(i - 2); |
| 56 | + } |
| 57 | + |
| 58 | + // steps.add(cost[cost.length - 1]); |
| 59 | + // console.log([ |
| 60 | + // price[0], |
| 61 | + // ...Array.from(path).map((e) => cost[e]), |
| 62 | + // cost[cost.length - 1], |
| 63 | + // ]); |
| 64 | + // console.log(cost); |
| 65 | + |
| 66 | + console.log([...path, cost[cost.length - 1]]); |
| 67 | +}; |
| 68 | + |
| 69 | +// for (let i = 1; i < cost.length; i++) {} |
| 70 | +// // n = 8 stairs, k = 2 steps, |
| 71 | +minCostStairs([3, 2, 4, 6, 1, 1, 5, 3]); // => 0--3--2--4--6--1--1--5--3 |
| 72 | +// minCostStairs([1, 100, 1, 1, 1, 100, 1, 1, 100, 1, 0]); // |
| 73 | +// // https://leetcode.com/problems/min-cost-climbing-stairs/ |
| 74 | +// //////////////////////////////////////////////////////////////// |
| 75 | +// // LAST step's price !== 0 |
| 76 | +// //////////////////////////////////////////////////////////////// |
| 77 | + |
| 78 | +// //////////////////////////////////////////////////////////////// |
| 79 | +// // Framework for solving DP Problems |
| 80 | +// // 1. Define the objective function |
| 81 | +// //// f(n) is the cheapest way to get to the last step |
| 82 | +// // 2. Identify base cases, |
| 83 | +// ////([3, 2, 4]) => 0--3--2--4 |
| 84 | +// //// f(0) = 0 |
| 85 | +// //// f(1) = 3 |
| 86 | +// //// f(2) = 2 |
| 87 | +// //// f(3) = 6 |
| 88 | +// // 3. Write down: Recurrence Relation for the optimized objective function |
| 89 | +// //// f(n) = min(fn + fn-1, fn + fn-2) |
| 90 | + |
| 91 | +// // 4. What's the order of execution? |
| 92 | +// //// bottom-up |
| 93 | +// // 5. Where to look for the answer? |
| 94 | +// //// |
| 95 | +// //////////////////////////////////////////////////////////////// |
| 96 | +// const minCostStairs = (cost) => { |
| 97 | +// for (i = 2; i < cost.length; i++) { |
| 98 | +// cost[i] = cost[i] + Math.min(cost[i - 1], cost[i - 2]); |
| 99 | +// } |
| 100 | +// console.log(cost[cost.length - 1]); |
| 101 | +// return cost[cost.length - 1]; |
| 102 | +// }; |
| 103 | + |
| 104 | +// minCostStairs([3, 2, 4]); |
| 105 | +// minCostStairs([1, 100, 1, 1, 1, 100, 1, 1, 100, 1, 0]); |
0 commit comments