Skip to content

Commit 742400d

Browse files
committed
n stairs k steps find cheapest path
1 parent 4ebbddd commit 742400d

File tree

2 files changed

+114
-4
lines changed

2 files changed

+114
-4
lines changed

leetcode--21-MergeTwoSortedLists.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,17 @@ class ListNode {
2727
// l2 1 3 4
2828

2929
var mergeTwoLists = function (l1, l2) {
30-
let current = new ListNode();
31-
let prev = current;
30+
let newList = new ListNode();
31+
let prev = newList;
3232
let count = 0;
3333
while (l1 && l2) {
3434
count++;
35+
console.log(newList);
3536

3637
if (l1.val > l2.val) {
3738
prev.next = l2;
3839
prev = l2;
40+
// console.log(prev);
3941

4042
l2 = l2.next;
4143
} else {
@@ -44,10 +46,13 @@ var mergeTwoLists = function (l1, l2) {
4446

4547
l1 = l1.next;
4648
}
47-
console.log(count, prev);
49+
console.log(newList);
50+
console.log("-------------------------");
51+
52+
// console.log(count, prev);
4853
}
4954

50-
console.log(current);
55+
// console.log(newList);
5156
};
5257

5358
const a = new ListNode(1);

w-dp-stairsKSteps-leetcode746.js

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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

Comments
 (0)