-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstructy-112-positioningPlants-2.js
143 lines (115 loc) · 3.94 KB
/
structy-112-positioningPlants-2.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// p: array 2d
// r: num
// e:
// costs = [
// t j
// c [4, 3, 7], [4, 3, 7],
// i [6, 1, 9], [9, 5, 12],
// [2, 5, 3] [7, 14, 8]
// ]
// [1 2]
// [5 4] => 5
// arr[i][j] += min arr[i-1][!j]
// dp recursion pos,lastPlant
// 0,NULL
// 4 / 3 | 7\
// 1,0 1,1 1,2
// 1/ 9\ 6/ 9\ 6/ 1 \
// 2,1 2,2 2,0 2,2 2,0 2,1
// 2/ 3 \ 2/ 5\ 5/ 3\ 2/ 5\ 5/ 3\ 2/ 3\
// 3.0 3.2 3.0 3.1 3.1 3.2 3.0 3.1 3.1 3.2 3.0 3.2
// // recursive W memo
// const positioningPlants = (costs, i = 0, lastPlant = null, memo = {}) => {
// const ij = i + "," + lastPlant;
// if (ij in memo) return memo[ij];
// if (i === costs.length) return 0;
// let min = Infinity;
// for (let j = 0; j < costs[0].length; j++) {
// if (j === lastPlant) continue;
// // min = costs[i][j] + Math.min(positioningPlants(costs, i + 1, j), min); >>>> WRONG
// min = Math.min(costs[i][j] + positioningPlants(costs, i + 1, j, memo), min);
// }
// memo[ij] = min;
// return min;
// };
// recursive WO memo
// const positioningPlants = (costs, i = 0, lastPlant = null) => {
// if (i === costs.length) return 0;
// let min = Infinity;
// for (let j = 0; j < costs[0].length; j++) {
// if (j === lastPlant) continue;
// // min = costs[i][j] + Math.min(positioningPlants(costs, i + 1, j), min); >>>> WRONG
// min = Math.min(costs[i][j] + positioningPlants(costs, i + 1, j), min);
// }
// return min;
// };
// // dp iteration
// const positioningPlants = (costs) => {
// for (let i = 1; i < costs.length; i++) {
// for (let j = 0; j < costs[0].length; j++) {
// const lastPlant = j;
// const min = Infinity;
// for (let k = 0; k < costs[0].length; k++) {
// if (k === lastPlant) continue;
// min = Math.min(min, costs[i][k]);
// }
// costs[i][j] += min;
// }
// }
// return Math.min(...costs[costs.length - 1]);
// };
// // // dp iteration O(n^m)
// const positioningPlants = (grid) => {
// for (let i = 1; i < grid.length; i++) {
// for (let j = 0; j < grid[0].length; j++) {
// let min = Infinity;
// for (let k = 0; k < grid[0].length; k++) {
// if (k === j) continue;
// min = Math.min(min, grid[i - 1][k]);
// }
// grid[i][j] += min;
// }
// }
// return Math.min(...grid[grid.length - 1]);
// };
// curr,last
// 0,null
// 4/ 3\ 7\
// 1,0 . 1,1 1,2
// /1 \9 /6 \9 /6 \1
// 2,1 2,2 2,0 2,2 2,0 2,1
// 2/ 3\ /2 5\
// 3,0 3,2 3,0 3,1
// w MEMO
const positioningPlants = (grid, curr = 0, last = null, memo = {}) => {
const currlast = curr + "," + last;
if (currlast in memo) return memo[currlast];
if (curr === grid.length) return 0;
let min = Infinity;
for (let i = 0; i < grid[0].length; i++) {
if (i === last) continue;
min = Math.min(
min,
grid[curr][i] + positioningPlants(grid, curr + 1, i, memo)
);
}
memo[currlast] = min;
return min;
};
// // no MEMO
// const positioningPlants = (grid, curr = 0, last = null) => {
// if (curr === grid.length) return 0;
// let min = Infinity;
// for (let i = 0; i < grid[0].length; i++) {
// if (i === last) continue;
// min = Math.min(min, grid[curr][i] + positioningPlants(grid, curr + 1, i));
// }
// return min;
// };
console.log(
positioningPlants([
[4, 3, 7],
[6, 1, 9],
[2, 5, 3],
])
); // -> 7, by doing 4 + 1 + 2.