-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstructy-077-maxPathSum.js
72 lines (59 loc) · 1.75 KB
/
structy-077-maxPathSum.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// https://structy.net/problems/max-path-sum
// p: grid
// r: num
//
//
////////////////////////////////////////////////////////////////
// Framework for solving DP Problems
// 1. Define the objective function
//// f(n) is max sum travel
// 2. Identify base cases
//// y=0, x=0 => f(y,x)= f(y,x)
// 3. Write down: Recurrence Relation for the optimized objective function
// f(y,x) = max(f(y-1,x), f(y,x-1))
// 4. What's the order of execution?
//// bottom-up
// 5. Where to look for the answer?
//// f(n)
////////////////////////////////////////////////////////////////
// // recursion
// const maxPathSum = (grid, y = 0, x = 0, memo = {}) => {
// if (y === grid.length || x === grid[0].length) return -Infinity;
// if (y === grid.length - 1 && x === grid[0].length - 1) return grid[y][x];
// const current = y + "," + x;
// if (current in memo) return memo[current];
// console.log(memo);
// memo[current] =
// grid[y][x] +
// Math.max(
// maxPathSum(grid, y + 1, x, memo),
// maxPathSum(grid, y, x + 1, memo)
// );
// return memo[current];
// };
iteration;
const maxPathSum = (grid) => {
for (let y = 0; y < grid.length; y++) {
for (let x = 0; x < grid[0].length; x++) {
if (y > 0 && x > 0) {
grid[y][x] = grid[y][x] + Math.max(grid[y][x - 1], grid[y - 1][x]);
} else if (y > 0) {
grid[y][x] = grid[y][x] + grid[y - 1][x];
} else if (x > 0) {
grid[y][x] = grid[y][x] + grid[y][x - 1];
}
}
}
return grid[(grid.length - 1, grid[0].length - 1)];
};
const grid = [
[1, 3, 12],
[5, 1, 1],
[3, 6, 1],
];
console.log(maxPathSum(grid)); // -> 18
// [
// [1, 4, 16],
// [6, 7, 17],
// [9, 15, 18],
// ];