[Solved] Number of Increasing Paths in a Grid LeetCode Contest Problem

Number of Increasing Paths in a Grid: You are given an m x n integer matrix grid, where you can move from a cell to any adjacent cell in all 4 directions.

Return the number of strictly increasing paths in the grid such that you can start from any cell and end at any cell. Since the answer may be very large, return it modulo 109 + 7.

Two paths are considered different if they do not have exactly the same sequence of visited cells.

Number of Increasing Paths in a Grid LeetCode Contest

Example 1:

[Solved] Number of Increasing Paths in a Grid LeetCode Contest Problem
Input: grid = [[1,1],[3,4]]
Output: 8
Explanation: The strictly increasing paths are:
- Paths with length 1: [1], [1], [3], [4].
- Paths with length 2: [1 -> 3], [1 -> 4], [3 -> 4].
- Paths with length 3: [1 -> 3 -> 4].
The total number of paths is 4 + 3 + 1 = 8.

Example 2:

Input: grid = [[1],[2]]
Output: 3
Explanation: The strictly increasing paths are:
- Paths with length 1: [1], [2].
- Paths with length 2: [1 -> 2].
The total number of paths is 2 + 1 = 3.

Constraints:

  • m == grid.length
  • n == grid[i].length
  • 1 <= m, n <= 1000
  • 1 <= m * n <= 105
  • 1 <= grid[i][j] <= 105

Solution

Because we want strictly increasing paths,
there is no cycle of the path.
We can iterative all A[i][j] in increasing order.

Explanation

dp[i][j] means the number of paths ending at A[i][j].
We initial all dp[i][j] = 1.

Because we want strictly increasing paths, there is no cycle of path.
We iterative all A[i][j] in increasing order,
d[i][j] += dp[x][y] from smaller neighbours.

Finally, return the sum of dp.

Complexity

Time O(mn^2log(m+n))
Space O(mn)

Method 1:

Python

    def countPaths(self, A):
        m, n, mod = len(A), len(A[0]), 10 ** 9 + 7
        dp = [[1] * n for i in range(m)]
        for a, i, j in sorted([A[i][j], i, j] for i in range(m) for j in range(n)):
            for x, y in [[i, j + 1], [i, j - 1], [i + 1, j], [i - 1, j]]:
                if 0 <= x < m and 0 <= y < n and A[x][y] < A[i][j]:
                    dp[i][j] += dp[x][y] % mod
        return sum(map(sum, dp)) % mod
class Solution {
public:
    const int M=1e9+7;
    long long ans=0;
    int m,n;
    vector<int> dx={0,1,0,-1};
    vector<int> dy={1,0,-1,0};
    long long modadd(long a,long b){return (a%M+b%M)%M;}

    int func(int i,int j,vector<vector<int>> &grid,vector<vector<long long>> &dp)
    {
        for(int k=0;k<4;k++)
        {
            int newx=i+dx[k];
            int newy=j+dy[k];
            if(newx<0 || newy<0 || newx>=m || newy>=n)continue;
            if(grid[newx][newy]>grid[i][j])
            {
            if(dp[newx][newy]==0)
            {
                dp[i][j]=modadd(dp[i][j],func(newx,newy,grid,dp));
            }
            else
                dp[i][j]=modadd(dp[i][j],dp[newx][newy]);
            }
        }        
        dp[i][j]=modadd(dp[i][j],1);
        return dp[i][j];
    }
    int countPaths(vector<vector<int>>& grid) 
    {
        m=grid.size();
        n=grid[0].size();
        vector<vector<long long>> dp(m,vector<long long>(n,0));
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(dp[i][j]==0)
                    int lmao=func(i,j,grid,dp);
            }
        }
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<n;j++)
                ans=modadd(ans,dp[i][j]);
        }
        return ans%M;
    }
};
class Solution {
int path[][];
int mod=(int)Math.pow(10,9)+7;
public int countPaths(int[][] grid) {
int n=grid.length;
int m=grid[0].length;

    int ans=0;
    path=new int[n][m];
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
           
            ans=(ans%mod+ dfs(grid,i,j,m,n,-1)%mod)%mod;
            
        }
    }
    return ans;
}
public int dfs(int[][] grid,int x,int y,int m,int n,int pre){
    if(x<0 || y<0 || x>=n || y>=m)return 0;
    if(grid[x][y]<=pre)return 0;
    if(path[x][y]!=0)return path[x][y];
   
    int l1=dfs(grid,x+1,y,m,n,grid[x][y]);
    int l2=dfs(grid,x-1,y,m,n,grid[x][y]);
    int l3=dfs(grid,x,y+1,m,n,grid[x][y]);
    int l4=dfs(grid,x,y-1,m,n,grid[x][y]);
    
    return  path[x][y]=(1+l1+l2+l3+l4)%mod;
}  
}

Method 2: DFS

Time O(mn)
Space O(mn)

    def countPaths(self, A):
        m, n, mod = len(A), len(A[0]), 10 ** 9 + 7

        @lru_cache(None)
        def count(i, j):
            res = 1
            for x, y in [[i, j + 1], [i, j - 1], [i + 1, j], [i - 1, j]]:
                if 0 <= x < m and 0 <= y < n and A[x][y] < A[i][j]:
                    res += count(x, y) % mod
            return res

        return sum(count(i,j) for i in range(m) for j in range(n)) % mod

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *