Skip to content

Commit fb9a53f

Browse files
committed
dp unique paths - find max profit - find best path with DFS
1 parent 632765a commit fb9a53f

File tree

2 files changed

+148
-4
lines changed

2 files changed

+148
-4
lines changed

leetcode-62-UniquePaths.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,19 @@ const uniquePaths = function (m, n, red) {
7474
console.log(arr);
7575

7676
// console.log(arr[m - 1][n - 1]);
77-
return arr[m - 1][n - 1];
77+
// return arr[m - 1][n - 1];
7878
};
7979

80-
uniquePaths(7, 3, [
80+
// uniquePaths(7, 3, [
81+
// [1, 2],
82+
// [5, 1],
83+
// [3, 0],
84+
// ]);
85+
86+
uniquePaths(3, 4, [
87+
[1, 1],
8188
[1, 2],
82-
[5, 1],
83-
[3, 0],
89+
[1, 3],
8490
]);
8591

8692
// // WITHOUT RED SQUARE
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
// 1. objective func: f(n) return max profit
2+
// 2. base cases:
3+
// f([1,1]) = 1
4+
// f([2,2]) = 2
5+
// 3. recurrent func:
6+
// f(y,x) = f[y][x] + Math.max(f(y-1,x) + f(x-1,y))
7+
8+
// 4. order:
9+
// bottom up
10+
// 5. result: f(y-1,x-1)
11+
12+
[
13+
[0, 2, 4, 5],
14+
[3, 4, 5, 6],
15+
[7, 11, 13, 13],
16+
];
17+
////////////////////////////////////// FIND MAX PROFIT
18+
const traceMaxProfit = (grid) => {
19+
// get max profit
20+
for (y = 0; y < grid.length; y++) {
21+
for (x = 0; x < grid[y].length; x++) {
22+
grid[y][x] === "S" && (grid[y][x] = 0);
23+
grid[y][x] === "E" && (grid[y][x] = 0);
24+
25+
if (grid[y - 1] === undefined && grid[y][x - 1] === undefined) {
26+
continue;
27+
} else if (grid[y - 1] === undefined) {
28+
grid[y][x] += grid[y][x - 1];
29+
} else if (grid[y][x - 1] === undefined) {
30+
grid[y][x] += grid[y - 1][x];
31+
} else {
32+
grid[y][x] += Math.max(grid[y - 1][x], grid[y][x - 1]);
33+
}
34+
}
35+
}
36+
37+
// trace back to find path
38+
39+
const stack = [[grid.length - 1, grid[0].length - 1]];
40+
41+
while (stack.length > 0) {
42+
console.log(stack);
43+
44+
let [y, x] = stack.pop();
45+
46+
if (y === 0 && x === 0) {
47+
grid[y][x] = "x";
48+
break;
49+
}
50+
51+
if (y === 0) {
52+
grid[y][x] = "x";
53+
stack.push([y, x - 1]);
54+
continue;
55+
}
56+
57+
if (x === 0) {
58+
grid[y][x] = "x";
59+
stack.push([y - 1, x]);
60+
continue;
61+
}
62+
63+
if (y > 0 && x > 0) {
64+
grid[y - 1][x] > grid[y][x - 1] &&
65+
(grid[y - 1][x] = "x") &&
66+
stack.push([y - 1, x]);
67+
grid[y - 1][x] < grid[y][x - 1] &&
68+
(grid[y][x - 1] = "x") &&
69+
stack.push([y, x - 1]);
70+
continue;
71+
}
72+
}
73+
74+
console.log(stack);
75+
console.log(grid);
76+
// for (y = grid.length - 1; y >= 0; y--) {
77+
// for (x = grid[y].length - 1; x >= 0; x--) {
78+
// if (y === 0 && x === 0) {
79+
// grid[y][x] = "x";
80+
// } else if (y === 0) {
81+
// // grid[y][x] = "x";
82+
// // continue;
83+
// } else if (x === 0) {
84+
// // grid[y][x] = "x";
85+
// // continue;
86+
// } else {
87+
// grid[y - 1][x] > grid[y][x - 1] && (grid[y - 1][x] = "x");
88+
// grid[y - 1][x] < grid[y][x - 1] && (grid[y][x - 1] = "x");
89+
// }
90+
// }
91+
// }
92+
93+
console.log(grid);
94+
return grid;
95+
};
96+
97+
// const gridT = [
98+
// ["S", 2, 2, 1],
99+
// [3, 1, 1, 1],
100+
// [4, 4, 2, "E"],
101+
// ];
102+
const gridT = [
103+
["S", 2, 2, 1],
104+
[3, 1, 1, 1],
105+
[4, 4, 2, "E"],
106+
];
107+
108+
traceMaxProfit(gridT);
109+
//
110+
// ////////////////////////////////////// FIND MAX PROFIT
111+
// const maxProfit = (grid) => {
112+
// for (y = 0; y < grid.length; y++) {
113+
// for (x = 0; x < grid[y].length; x++) {
114+
// grid[y][x] === "S" && (grid[y][x] = 0);
115+
// grid[y][x] === "E" && (grid[y][x] = 0);
116+
117+
// if (grid[y - 1] === undefined && grid[y][x - 1] === undefined) {
118+
// continue;
119+
// } else if (grid[y - 1] === undefined) {
120+
// grid[y][x] += grid[y][x - 1];
121+
// } else if (grid[y][x - 1] === undefined) {
122+
// grid[y][x] += grid[y - 1][x];
123+
// console.log("222");
124+
// } else {
125+
// grid[y][x] += Math.max(grid[y - 1][x], grid[y][x - 1]);
126+
// }
127+
// }
128+
// }
129+
// console.log(grid[y - 1][x - 1]);
130+
// return grid[y - 1][x - 1];
131+
// };
132+
133+
// const grid = [
134+
// ["S", 2, 2, 1],
135+
// [3, 1, 1, 1],
136+
// [4, 4, 2, "E"],
137+
// ];
138+
// maxProfit(grid);

0 commit comments

Comments
 (0)