-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCherryPickup.js
More file actions
126 lines (106 loc) · 3.4 KB
/
Copy pathCherryPickup.js
File metadata and controls
126 lines (106 loc) · 3.4 KB
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
// https://leetcode-cn.com/problems/cherry-pickup/
var Test = require('../Common/Test');
var Matrix = require('../Common/Matrix').Matrix;
var cherryPickup = function (grid) {
const n = grid.length;
if (n == 0) return 0;
if (n == 1) return grid[0][0];
let dp = [[grid[0][0]]];
console.log(dp);
for (let step = 1; step < n * 2 - 1; step++) {
console.log({ step });
const dp2 = Array(n).fill(0).map(a => Array(n).fill(Number.MIN_SAFE_INTEGER));
// const dp2 = Array(n).fill(0).map(a => Array(n).fill(-1));
for (let x1 = Math.max(0, step - n + 1); x1 < Math.min(step + 1, n); x1++) {
const y1 = step - x1;
console.log({ x1, y1 });
for (let x2 = Math.max(0, step - n + 1); x2 <= x1; x2++) {
const y2 = step - x2;
console.log({ x2, y2 });
if (grid[y1][x1] != -1 && grid[y2][x2] != -1) {
const val = grid[y1][x1] + (x1 != x2 ? grid[y2][x2] : 0);
// let max = 0;
// let max = -1;
let max = Number.MIN_SAFE_INTEGER;
for (let i = x1 - 1; i <= x1; i++) {
if (i >= 0 && i < dp.length) {
for (let j = x2 - 1; j <= x2; j++) {
if (j >= 0 && j < dp[i].length) {
max = Math.max(max, dp[i][j]);
// dp2[x1][x2] = Math.max(dp2[x1][x2], dp[i][j] + val);
}
}
}
}
dp2[x1][x2] = max + val;
}
}
}
dp = dp2;
console.log(dp);
}
console.log(dp);
return Math.max(0, dp[n - 1][n - 1]);
};
function test(input) {
Matrix.logMatrixInArray(input);
const test = new Test.Test(cherryPickup, input);
test.logArgs = false;
test.do();
}
input1 = [
[0, 1, -1],
[1, 0, -1],
[1, 1, 1]];
input2 = [
[1, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 1],
[1, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 1]];
input22 = [
[1, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 1],
[0, 0, 0, 1, 0, 0, 0],
[1, 0, 0, 1, 0, 0, 0],
[1, 0, 0, 1, 0, 0, 1],
[1, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 1]];
input3 = [
[1, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0],
[1, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 1],
[0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 1]];
input33 = [
[1, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0],
[1, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 1],
[0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 0],
[0, 0, 0, 1, 0, 0, 1]];
// test(input1);
// test(input2);
// test(input22);
// test(input3);
// test(input33);
test([[1, 1, -1], [1, -1, 1], [-1, 1, 1]]);
function testTriangleArray() {
const array = [];
for (let i = 0; i < 5; i++) {
const a = [];
array.push({ i, array: a });
for (let j = 0; j <= i; j++) {
a.push(j);
}
}
console.log(array);
}
// testTriangleArray();
// [[1, 1, 1, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 1], [1, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 1, 1, 1]]