forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cherry-pickup.cpp
39 lines (37 loc) · 1.57 KB
/
cherry-pickup.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
37
38
39
// Time: O(n^3)
// Space: O(n^2)
class Solution {
public:
int cherryPickup(vector<vector<int>>& grid) {
// dp holds the max # of cherries two k-length paths can pickup.
// The two k-length paths arrive at (i, k - i) and (j, k - j),
// respectively.
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) { // 0 <= i < n, 0 <= k-i < n
for (int j = min(k , n - 1); j >= i; --j) { // i <= j < n, 0 <= k-j < n
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);
}
};