@@ -56,16 +56,30 @@ The total number of cherries picked up is 5, and this is the maximum possible.
56
56
57
57
## Solutions
58
58
59
- ### Solution 1
59
+ ### Solution 1: Dynamic Programming
60
+
61
+ According to the problem description, the player starts from $(0, 0)$, reaches $(n-1, n-1)$, and then returns to the starting point $(0, 0)$. We can consider the player as starting from $(0, 0)$ to $(n-1, n-1)$ twice.
62
+
63
+ Therefore, we define $f[ k] [ i_1 ] [ i_2] $ as the maximum number of cherries that can be picked when both have walked $k$ steps and reached $(i_1, k-i_1)$ and $(i_2, k-i_2)$ respectively. Initially, $f[ 0] [ 0 ] [ 0] = grid[ 0] [ 0 ] $. The initial values of other $f[ k] [ i_1 ] [ i_2] $ are negative infinity. The answer is $\max(0, f[ 2n-2] [ n-1 ] [ n-1] )$.
64
+
65
+ According to the problem description, we can get the state transition equation:
66
+
67
+ $$
68
+ f[k][i_1][i_2] = \max(f[k-1][x_1][x_2] + t, f[k][i_1][i_2])
69
+ $$
70
+
71
+ Where $t$ represents the number of cherries at positions $(i_1, k-i_1)$ and $(i_2, k-i_2)$, and $x_1, x_2$ represent the previous step positions of $(i_1, k-i_1)$ and $(i_2, k-i_2)$ respectively.
72
+
73
+ The time complexity is $O(n^3)$, and the space complexity is $O(n^3)$. Where $n$ is the side length of the grid.
60
74
61
75
<!-- tabs:start -->
62
76
63
77
``` python
64
78
class Solution :
65
79
def cherryPickup (self , grid : List[List[int ]]) -> int :
66
80
n = len (grid)
67
- dp = [[[- inf] * n for _ in range (n)] for _ in range ((n << 1 ) - 1 )]
68
- dp [0 ][0 ][0 ] = grid[0 ][0 ]
81
+ f = [[[- inf] * n for _ in range (n)] for _ in range ((n << 1 ) - 1 )]
82
+ f [0 ][0 ][0 ] = grid[0 ][0 ]
69
83
for k in range (1 , (n << 1 ) - 1 ):
70
84
for i1 in range (n):
71
85
for i2 in range (n):
@@ -83,23 +97,21 @@ class Solution:
83
97
for x1 in range (i1 - 1 , i1 + 1 ):
84
98
for x2 in range (i2 - 1 , i2 + 1 ):
85
99
if x1 >= 0 and x2 >= 0 :
86
- dp[k][i1][i2] = max (
87
- dp[k][i1][i2], dp[k - 1 ][x1][x2] + t
88
- )
89
- return max (0 , dp[- 1 ][- 1 ][- 1 ])
100
+ f[k][i1][i2] = max (f[k][i1][i2], f[k - 1 ][x1][x2] + t)
101
+ return max (0 , f[- 1 ][- 1 ][- 1 ])
90
102
```
91
103
92
104
``` java
93
105
class Solution {
94
106
public int cherryPickup (int [][] grid ) {
95
107
int n = grid. length;
96
- int [][][] dp = new int [n * 2 ][n][n];
97
- dp [0 ][0 ][0 ] = grid[0 ][0 ];
108
+ int [][][] f = new int [n * 2 ][n][n];
109
+ f [0 ][0 ][0 ] = grid[0 ][0 ];
98
110
for (int k = 1 ; k < n * 2 - 1 ; ++ k) {
99
111
for (int i1 = 0 ; i1 < n; ++ i1) {
100
112
for (int i2 = 0 ; i2 < n; ++ i2) {
101
113
int j1 = k - i1, j2 = k - i2;
102
- dp [k][i1][i2] = Integer . MIN_VALUE ;
114
+ f [k][i1][i2] = Integer . MIN_VALUE ;
103
115
if (j1 < 0 || j1 >= n || j2 < 0 || j2 >= n || grid[i1][j1] == - 1
104
116
|| grid[i2][j2] == - 1 ) {
105
117
continue ;
@@ -111,14 +123,14 @@ class Solution {
111
123
for (int x1 = i1 - 1 ; x1 <= i1; ++ x1) {
112
124
for (int x2 = i2 - 1 ; x2 <= i2; ++ x2) {
113
125
if (x1 >= 0 && x2 >= 0 ) {
114
- dp [k][i1][i2] = Math . max(dp [k][i1][i2], dp [k - 1 ][x1][x2] + t);
126
+ f [k][i1][i2] = Math . max(f [k][i1][i2], f [k - 1 ][x1][x2] + t);
115
127
}
116
128
}
117
129
}
118
130
}
119
131
}
120
132
}
121
- return Math . max(0 , dp [n * 2 - 2 ][n - 1 ][n - 1 ]);
133
+ return Math . max(0 , f [n * 2 - 2 ][n - 1 ][n - 1 ]);
122
134
}
123
135
}
124
136
```
@@ -128,42 +140,49 @@ class Solution {
128
140
public:
129
141
int cherryPickup(vector<vector<int >>& grid) {
130
142
int n = grid.size();
131
- vector<vector<vector<int >>> dp (n << 1, vector<vector<int >>(n, vector<int >(n, -1e9)));
132
- dp [ 0] [ 0 ] [ 0] = grid[ 0] [ 0 ] ;
143
+ vector<vector<vector<int >>> f (n << 1, vector<vector<int >>(n, vector<int >(n, -1e9)));
144
+ f [ 0] [ 0 ] [ 0] = grid[ 0] [ 0 ] ;
133
145
for (int k = 1; k < n * 2 - 1; ++k) {
134
146
for (int i1 = 0; i1 < n; ++i1) {
135
147
for (int i2 = 0; i2 < n; ++i2) {
136
148
int j1 = k - i1, j2 = k - i2;
137
- if (j1 < 0 || j1 >= n || j2 < 0 || j2 >= n || grid[ i1] [ j1 ] == -1 || grid[ i2] [ j2 ] == -1) continue;
149
+ if (j1 < 0 || j1 >= n || j2 < 0 || j2 >= n || grid[ i1] [ j1 ] == -1 || grid[ i2] [ j2 ] == -1) {
150
+ continue;
151
+ }
138
152
int t = grid[ i1] [ j1 ] ;
139
- if (i1 != i2) t += grid[ i2] [ j2 ] ;
140
- for (int x1 = i1 - 1; x1 <= i1; ++x1)
141
- for (int x2 = i2 - 1; x2 <= i2; ++x2)
142
- if (x1 >= 0 && x2 >= 0)
143
- dp[ k] [ i1 ] [ i2] = max(dp[ k] [ i1 ] [ i2] , dp[ k - 1] [ x1 ] [ x2] + t);
153
+ if (i1 != i2) {
154
+ t += grid[ i2] [ j2 ] ;
155
+ }
156
+ for (int x1 = i1 - 1; x1 <= i1; ++x1) {
157
+ for (int x2 = i2 - 1; x2 <= i2; ++x2) {
158
+ if (x1 >= 0 && x2 >= 0) {
159
+ f[ k] [ i1 ] [ i2] = max(f[ k] [ i1 ] [ i2] , f[ k - 1] [ x1 ] [ x2] + t);
160
+ }
161
+ }
162
+ }
144
163
}
145
164
}
146
165
}
147
- return max(0, dp [ n * 2 - 2] [ n - 1 ] [ n - 1] );
166
+ return max(0, f [ n * 2 - 2] [ n - 1 ] [ n - 1] );
148
167
}
149
168
};
150
169
```
151
170
152
171
```go
153
172
func cherryPickup(grid [][]int) int {
154
173
n := len(grid)
155
- dp := make([][][]int, (n<<1)-1)
156
- for i := range dp {
157
- dp [i] = make([][]int, n)
158
- for j := range dp [i] {
159
- dp [i][j] = make([]int, n)
174
+ f := make([][][]int, (n<<1)-1)
175
+ for i := range f {
176
+ f [i] = make([][]int, n)
177
+ for j := range f [i] {
178
+ f [i][j] = make([]int, n)
160
179
}
161
180
}
162
- dp [0][0][0] = grid[0][0]
181
+ f [0][0][0] = grid[0][0]
163
182
for k := 1; k < (n<<1)-1; k++ {
164
183
for i1 := 0; i1 < n; i1++ {
165
184
for i2 := 0; i2 < n; i2++ {
166
- dp [k][i1][i2] = int(-1e9)
185
+ f [k][i1][i2] = int(-1e9)
167
186
j1, j2 := k-i1, k-i2
168
187
if j1 < 0 || j1 >= n || j2 < 0 || j2 >= n || grid[i1][j1] == -1 || grid[i2][j2] == -1 {
169
188
continue
@@ -175,14 +194,50 @@ func cherryPickup(grid [][]int) int {
175
194
for x1 := i1 - 1; x1 <= i1; x1++ {
176
195
for x2 := i2 - 1; x2 <= i2; x2++ {
177
196
if x1 >= 0 && x2 >= 0 {
178
- dp [k][i1][i2] = max(dp [k][i1][i2], dp [k-1][x1][x2]+t)
197
+ f [k][i1][i2] = max(f [k][i1][i2], f [k-1][x1][x2]+t)
179
198
}
180
199
}
181
200
}
182
201
}
183
202
}
184
203
}
185
- return max(0, dp[n*2-2][n-1][n-1])
204
+ return max(0, f[n*2-2][n-1][n-1])
205
+ }
206
+ ```
207
+
208
+ ``` ts
209
+ function cherryPickup(grid : number [][]): number {
210
+ const n: number = grid .length ;
211
+ const f: number [][][] = Array .from ({ length: n * 2 - 1 }, () =>
212
+ Array .from ({ length: n }, () => Array .from ({ length: n }, () => - Infinity )),
213
+ );
214
+ f [0 ][0 ][0 ] = grid [0 ][0 ];
215
+ for (let k = 1 ; k < n * 2 - 1 ; ++ k ) {
216
+ for (let i1 = 0 ; i1 < n ; ++ i1 ) {
217
+ for (let i2 = 0 ; i2 < n ; ++ i2 ) {
218
+ const [j1, j2]: [number , number ] = [k - i1 , k - i2 ];
219
+ if (
220
+ j1 < 0 ||
221
+ j1 >= n ||
222
+ j2 < 0 ||
223
+ j2 >= n ||
224
+ grid [i1 ][j1 ] == - 1 ||
225
+ grid [i2 ][j2 ] == - 1
226
+ ) {
227
+ continue ;
228
+ }
229
+ const t: number = grid [i1 ][j1 ] + (i1 != i2 ? grid [i2 ][j2 ] : 0 );
230
+ for (let x1 = i1 - 1 ; x1 <= i1 ; ++ x1 ) {
231
+ for (let x2 = i2 - 1 ; x2 <= i2 ; ++ x2 ) {
232
+ if (x1 >= 0 && x2 >= 0 ) {
233
+ f [k ][i1 ][i2 ] = Math .max (f [k ][i1 ][i2 ], f [k - 1 ][x1 ][x2 ] + t );
234
+ }
235
+ }
236
+ }
237
+ }
238
+ }
239
+ }
240
+ return Math .max (0 , f [n * 2 - 2 ][n - 1 ][n - 1 ]);
186
241
}
187
242
```
188
243
@@ -193,19 +248,14 @@ func cherryPickup(grid [][]int) int {
193
248
*/
194
249
var cherryPickup = function (grid ) {
195
250
const n = grid .length ;
196
- let dp = new Array (n * 2 - 1 );
197
- for (let k = 0 ; k < dp .length ; ++ k) {
198
- dp[k] = new Array (n);
199
- for (let i = 0 ; i < n; ++ i) {
200
- dp[k][i] = new Array (n).fill (- 1e9 );
201
- }
202
- }
203
- dp[0 ][0 ][0 ] = grid[0 ][0 ];
251
+ const f = Array .from ({ length: n * 2 - 1 }, () =>
252
+ Array .from ({ length: n }, () => Array .from ({ length: n }, () => - Infinity )),
253
+ );
254
+ f[0 ][0 ][0 ] = grid[0 ][0 ];
204
255
for (let k = 1 ; k < n * 2 - 1 ; ++ k) {
205
256
for (let i1 = 0 ; i1 < n; ++ i1) {
206
257
for (let i2 = 0 ; i2 < n; ++ i2) {
207
- const j1 = k - i1,
208
- j2 = k - i2;
258
+ const [j1 , j2 ] = [k - i1, k - i2];
209
259
if (
210
260
j1 < 0 ||
211
261
j1 >= n ||
@@ -216,21 +266,18 @@ var cherryPickup = function (grid) {
216
266
) {
217
267
continue ;
218
268
}
219
- let t = grid[i1][j1];
220
- if (i1 != i2) {
221
- t += grid[i2][j2];
222
- }
269
+ const t = grid[i1][j1] + (i1 != i2 ? grid[i2][j2] : 0 );
223
270
for (let x1 = i1 - 1 ; x1 <= i1; ++ x1) {
224
271
for (let x2 = i2 - 1 ; x2 <= i2; ++ x2) {
225
272
if (x1 >= 0 && x2 >= 0 ) {
226
- dp [k][i1][i2] = Math .max (dp [k][i1][i2], dp [k - 1 ][x1][x2] + t);
273
+ f [k][i1][i2] = Math .max (f [k][i1][i2], f [k - 1 ][x1][x2] + t);
227
274
}
228
275
}
229
276
}
230
277
}
231
278
}
232
279
}
233
- return Math .max (0 , dp [n * 2 - 2 ][n - 1 ][n - 1 ]);
280
+ return Math .max (0 , f [n * 2 - 2 ][n - 1 ][n - 1 ]);
234
281
};
235
282
```
236
283
0 commit comments