-
Notifications
You must be signed in to change notification settings - Fork 359
/
Copy pathCherryPickup.cpp
36 lines (35 loc) · 1.43 KB
/
CherryPickup.cpp
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
// URL:https://leetcode.com/problems/cherry-pickup/
// Time: O(n^3)
// Space: O(n^2)
class Solution {
public:
int cherryPickup(vector<vector<int>>& grid) {
const int n = grid.size();
vector<vector<int>> dp(n, vector<int>(n, -1));
dp[0][0] = grid[0][0];
const int max_len = 2 * (n - 1);
for (int k = 1; k <= max_len; ++k) {
for (int i = min(k, n - 1); i >= max(0, k - n + 1); --i) {
for (int j = min(k , n - 1); j >= i; --j) {
if (grid[i][k - i] == -1 ||
grid[j][k - j] == -1) {
dp[i][j] = -1;
continue;
}
int cnt = grid[i][k - i] + ((i == j) ? 0 : grid[j][k - j]);
int max_cnt = -1;
static const vector<pair<int, int>> directions{{0, 0}, {-1, 0}, {0, -1}, {-1, -1}};
for (const auto& direction : directions) {
const auto ii = i + direction.first;
const auto jj = j + direction.second;
if (ii >= 0 && jj >= 0 && dp[ii][jj] >= 0) {
max_cnt = max(max_cnt, dp[ii][jj] + cnt);
}
}
dp[i][j] = max_cnt;
}
}
}
return max(dp[n - 1][n - 1], 0);
}
};