Iterative DP explained
Intuition\nWe have to traverse all the possible paths by exploring the three transition,so we can use Dynamic programming\n\n# Approach\ndp[row][col]->maximum m
# Intuition\nWe have to traverse all the possible paths by exploring the three transition,so we can use Dynamic programming\n\n# Approach\ndp[row][col]->maximum moves till (row,col)\nExplore all the three possibilties an if u reach at a higher value\ndp[row][col]=1+dp[row+1][col+1](if u reach at this state),follow this for all the transitions and take maximum of all.\n\n# Complexity\n- Time complexity:\n$$O(n*m)$$ n is number of rows and m is number of columns\n\n- Space complexity:\n$$O(n*m)$$ n is number of rows and m is number of columns\n\n\nThis is what i thought during writing iterative DP  \n![WhatsApp Image 2024-10-29 at 19.50.10_7f38fb7d.jpg](https://assets.leetcode.com/users/images/39aa1b7e-c403-4068-981b-ddfe655e8376_1730211705.950774.jpeg)\n\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int maxMoves(vector<vector<int>>& grid) {\n        int n=grid.size();\n        int m=grid[0].size();\n        vector<vector<int>>dp(n,vector<int>(m,0));\n        // for(int i=0;i<n;i++){\n        //     dp[i][m-1]=1;\n        // }\n        for(int col=m-2;col>=0;col--){\n            for(int row=0;row<n;row++){\n                    if(row+1<n){\n                        if(grid[row][col]<grid[row+1][col+1]){\n                            dp[row][col]=max(dp[row][col],1+dp[row+1][col+1]);\n                        }\n                    }\n                    if(row-1>=0){\n                        if(grid[row][col]<grid[row-1][col+1]){\n                            dp[row][col]=max(dp[row][col],1+dp[row-1][col+1]);\n                        }  \n                    }\n                    if(grid[row][col]<grid[row][col+1]){\n                        dp[row][col]=max(dp[row][col],1+dp[row][col+1]);\n                    }\n                }\n            }\n            int ans=0;\n            for(int i=0;i<n;i++){\n                cout<<dp[i][0]<<endl;\n                ans=max(ans,dp[i][0]);\n            }\n            return ans;\n    }\n};\n```
['C++']