Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- int f(int i1,int i2,int j,vector<vector<int>> &grid,vector<vector<vector<int>>> &dp) {
- if(i1<0 or i2<0 or i1>=grid[0].size() or i2>=grid[0].size() or j>=grid.size()) return -1e9;
- if(j==grid.size()-1) {
- if(i1==i2) {
- return grid[j][i1];
- } else { return grid[j][i1] + grid[j][i2];}
- }
- int maxC = 0;
- if(i1==i2) {
- for(int i=-1;i<2;i++) {
- for(int m=-1;m<2;m++) {
- maxC =max(maxC, grid[j][i1] + f(i1+i,i2+m,j+1,grid,dp));
- }
- }
- } else {
- for(int i=-1;i<2;i++) {
- for(int m=-1;m<2;m++) {
- maxC =max(maxC, grid[j][i1] +grid[j][i2] + f(i1+i,i2+m,j+1,grid,dp));
- }
- }
- }
- return maxC;
- }
- int cherryPickup(vector<vector<int>>& grid) {
- // vector<int> moves{-1,0,1};
- int r = grid.size();
- int c = grid[0].size();
- vector<vector<vector<int>>> dp(c+1,vector<vector<int>>(c+1,vector<int>(r+1,-1)));
- return f(0,grid[0].size()-1,0,grid,dp);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement