Advertisement
Singasking

Untitled

Jan 17th, 2023
828
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. class Solution {
  2. public:
  3. int f(int i1,int i2,int j,vector<vector<int>> &grid,vector<vector<vector<int>>> &dp)  {
  4. if(i1<0 or i2<0 or i1>=grid[0].size() or i2>=grid[0].size() or j>=grid.size()) return -1e9;
  5.     if(j==grid.size()-1) {
  6.         if(i1==i2) {
  7.             return grid[j][i1];
  8.         } else { return grid[j][i1] + grid[j][i2];}
  9.     }
  10. int maxC = 0;
  11.     if(i1==i2) {
  12.        
  13.     for(int i=-1;i<2;i++) {
  14.         for(int m=-1;m<2;m++) {
  15.             maxC =max(maxC, grid[j][i1] + f(i1+i,i2+m,j+1,grid,dp));
  16.         }
  17.     }
  18.    
  19.     } else {
  20.        
  21.     for(int i=-1;i<2;i++) {
  22.         for(int m=-1;m<2;m++) {
  23.             maxC =max(maxC, grid[j][i1] +grid[j][i2] + f(i1+i,i2+m,j+1,grid,dp));
  24.         }
  25.     }
  26.    
  27. }
  28. return maxC;
  29. }
  30.     int cherryPickup(vector<vector<int>>& grid) {
  31.     //    vector<int> moves{-1,0,1};
  32.     int r = grid.size();
  33.     int c = grid[0].size();
  34.    vector<vector<vector<int>>> dp(c+1,vector<vector<int>>(c+1,vector<int>(r+1,-1)));
  35.         return f(0,grid[0].size()-1,0,grid,dp);
  36.     }
  37. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement