This is one of those problems where the clean idea matters more than the amount of code. For 1463. Cherry Pickup II, the solution in this repository is mainly a dynamic programming solution.
Guide
What?
The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: dynamic programming, two pointers.
The notes already sitting in the source point us in the right direction:
- DP
- @xiaowuc1's answer
- time: O(m*N^2), space: O(m*n^2)
Guide
When?
Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are cherryPickup.
Guide
Why?
The solution works because it narrows the problem until every update has a clear reason to exist.
- The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(m*N^2), space: O(m*n^2)
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//DP
02//@xiaowuc1's answer
03//Runtime: 280 ms, faster than 20.00% of C++ online submissions for Cherry Pickup II.
04//Memory Usage: 14.6 MB, less than 100.00% of C++ online submissions for Cherry Pickup II.
05//time: O(m*N^2), space: O(m*n^2)
06class Solution {
07public:
08 int cherryPickup(vector<vector<int>>& grid) {
09 int m = grid.size();
10 if(m == 0) return 0;
11 int n = grid[0].size();
12 if(n == 0) return 0;
13
14 /*
15 for example in the case:
16 [[0,8,7,10,9,10,0,9,6],
17 [8,7,10,8,7,4,9,6,10],
18 [8,1,1,5,1,5,5,1,2],
19 [9,4,10,8,8,1,9,5,0],
20 [4,3,6,10,9,2,4,8,10],
21 [7,3,2,8,3,3,5,9,8],
22 [1,2,6,5,6,2,0,10,0]]
23
24 dp[1][0][0] should be set to a invalid value, not 8,
25 because the two robots can't both appear at (1, 0)
26 to avoid this, we initialize dp to INT_MIN, not 0!
27 */
28
29
30 vector<vector<vector<int>>> dp(m, vector<vector<int>>(n, vector<int>(n, INT_MIN)));
31
32 dp[0][0][n-1] = grid[0][0] + grid[0][n-1];
33
34 for(int i = 1; i < m; i++){
35 for(int a = 0; a < n; a++){
36 for(int b = 0; b < n; b++){
37 for(int l = a-1; l <= a+1; l++){
38 for(int r = b-1; r <= b+1; r++){
39 if(l < 0 || l >= n || r < 0 || r >= n) continue;
40 dp[i][a][b] = max(dp[i][a][b], ((a != b) ? grid[i][a] + grid[i][b] : grid[i][a]) + dp[i-1][l][r]);
41 }
42 }
43
44 cout << i << ", " << a << ", " << b << ", " << dp[i][a][b] << endl;
45 }
46 }
47 }
48
49 int ans = 0;
50 for(int a = 0; a < n; a++){
51 for(int b = 0; b < n; b++){
52 ans = max(ans, dp[m-1][a][b]);
53 }
54 }
55
56 return ans;
57 }
58};
59
60//DP, O(n^2) space
61//@xiaowuc1's answer
62//Runtime: 228 ms, faster than 20.00% of C++ online submissions for Cherry Pickup II.
63//Memory Usage: 9.1 MB, less than 100.00% of C++ online submissions for Cherry Pickup II.
64//time: O(m*N^2), space: O(n^2)
65class Solution {
66public:
67 int cherryPickup(vector<vector<int>>& grid) {
68 int m = grid.size();
69 if(m == 0) return 0;
70 int n = grid[0].size();
71 if(n == 0) return 0;
72
73 vector<vector<vector<int>>> dp(2, vector<vector<int>>(n, vector<int>(n, INT_MIN)));
74
75 dp[0][0][n-1] = grid[0][0] + grid[0][n-1];
76
77 for(int i = 1; i < m; i++){
78 for(int a = 0; a < n; a++){
79 for(int b = 0; b < n; b++){
80 for(int l = a-1; l <= a+1; l++){
81 for(int r = b-1; r <= b+1; r++){
82 if(l < 0 || l >= n || r < 0 || r >= n) continue;
83 dp[i%2][a][b] = max(dp[i%2][a][b], ((a != b) ? grid[i][a] + grid[i][b] : grid[i][a]) + dp[(i-1)%2][l][r]);
84 }
85 }
86
87 // cout << i << ", " << a << ", " << b << ", " << dp[i%2][a][b] << endl;
88 }
89 }
90 }
91
92 int ans = 0;
93 for(int a = 0; a < n; a++){
94 for(int b = 0; b < n; b++){
95 ans = max(ans, dp[(m-1)%2][a][b]);
96 }
97 }
98
99 return ans;
100 }
101};
Cost