C++ Solution | Dp Recursion + Memoization and Tabulation bottom up
Tried solving the question using recursion and memoization in contest but two test cases were giving tle. Anyone have any suggetion to optimize the recusive dp 
Tried solving the question using recursion and memoization in contest but two test cases were giving tle. Anyone have any suggetion to optimize the recusive dp code please suggest.\n\n\nTried solving using tabulation bottom up dp and it passed all the test cases\n\n\nCode 1 ----> Recursion + Memoization\nCode 2 -----> Tabulation Bottom Up\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n\n\n\nCode 1:\n# Code\n```\nclass Solution {\npublic:\n   int m, n;\n   int helper(vector<vector<int>>& grid, int row, int col, vector<vector<int>>& dp){\n       if(row>=m or col>=n)\n           return 0;\n      \n      \n       if(dp[row][col]!=-1)\n           return dp[row][col];\n      \n       int res=INT_MIN/2;\n      \n       for(int i=row+1;i<m;i++){\n           int a=grid[i][col]-grid[row][col];\n           res=max(res, a);\n           int b=helper(grid, i, col, dp);\n          \n           if(a>0 and b>0){\n               res=max(res, a+b);\n           }\n       }\n      \n       for(int i=col+1;i<n;i++){\n           int a=grid[row][i]-grid[row][col];\n           res=max(res, a);\n           int b=helper(grid, row, i, dp);\n          \n           if(a>0 and b>0){\n               res=max(res, a+b);\n           }\n       }\n      \n       return dp[row][col]=res;\n   }\n   int maxScore(vector<vector<int>>& grid) {\n      \n      \n       m=grid.size();\n       n=grid[0].size();\n       vector<vector<int>> dp(m+1, vector<int>(n+1, -1));\n      \n       int res=INT_MIN;\n      \n       for(int i=0;i<m;i++){\n           for(int j=0;j<n;j++){\n               if(i==m-1 and j==n-1)\n                   continue;\n               int a=helper(grid, i, j, dp);\n               res=max(a, res);\n           }\n       }\n      \n       return res;\n      \n   }\n};\n\n\n```\nCode 2:\n\n\n```\nclass Solution {\npublic:\n   int maxScore(vector<vector<int>>& grid) {\n       int m=grid.size();\n       int n=grid[0].size();\n\n\n       vector<vector<int>> dp(m, vector<int>(n, INT_MIN));\n\n\n       dp[0][0]=grid[0][0];\n\n\n       // dp[m-1][n-2]=grid[m-1][n-1]-grid[m-1][n-2];\n       // dp[m-2][n-1]=grid[m-1][n-1]-grid[m-2][n-1];\n\n\n       int curr=grid[0][0];\n       int res=INT_MIN;\n\n\n       for(int i=1;i<m;i++){\n           dp[i][0]=max(dp[i][0], grid[i][0]-curr);\n           curr=min(grid[i][0], curr);\n           res=max(res, dp[i][0]);\n       }\n\n\n       curr=grid[0][0];\n       for(int i=1;i<n;i++){\n           dp[0][i]=max(dp[0][i], grid[0][i]-curr);\n           curr=min(grid[0][i], curr);\n           res=max(res, dp[0][i]);\n       }\n\n\n\n\n       for(int i=1;i<m;i++){\n           for(int j=1;j<n;j++){\n               int col = grid[i][j] - grid[i][j-1];\n               int row= grid[i][j] - grid[i-1][j];\n              \n               dp[i][j] = max(dp[i][j], max(col,row));\n               dp[i][j] = max(dp[i][j], max(col+dp[i][j-1], row+dp[i-1][j] ));\n              \n               res = max(res,dp[i][j]);\n           }\n       }\n\n\n       return res;\n   }\n};\n\n\n```
['C++']