329. Longest Increasing Path in a Matrix
Given an m x n integers matrix, return the length of the longest increasing path in matrix.
From each cell, you can either move in four directions: left, right, up, or down. You may not move diagonally or move outside the boundary (i.e., wrap-around is not allowed).
Example: 1
Input: matrix = [[9,9,4],[6,6,8],[2,1,1]]
Output: 4
Example: 2
Input: matrix = [[3,4,5],[3,2,6],[2,2,1]]
Output: 4
Example: 3
Input: matrix = [[1]]
Output: 1
            Constraints:
            
        - m == matrix.length
 - n == matrix[i].length
 - 1 <= m, n <= 200
 - 0 <= matrix[i][j] <= 2 ^ 31 - 1
 
             Step I (Making Observations): 
            
        - Important observations to make here is that, we can have atleast 1 as our answer, if we take only one block in path.
 - We can only from current cell to its neighbouring cell if and only if we the neighbouring cell has greater value than current cell.
 - Because of this we can see that there is only one way path, so the entire grid can be represented as a directed acyclic graph (DAG).
 - This problem is similar to finding the longest path in a DAG.
 - We need not create any graph explicitly here, we will solve it using recursion.
 
             Step II (Solving the problem using Recursion): 
            
        - Now for each cell [i, j], we will find the longest path that can be taken if this current cell is considered.
 - So the recursion goes this way: From current cell we will check whether we can go to any other neighbouring cell.
 - If YES we will go to that cell, and if NO we will return 1 (because we are considering the current cell).
 - This step is shown in code below.
 
            Solving using recursion (Will give TLE):
            
                int recur(int r, int c, int[][] matrix) {
                    int max = 0;
                    for(int i = 0; i < 4; i++) {
                        // dr = new int[] {1, 0, -1, 0}
                        // dc = new int[] {0, 1, 0, -1}
                        int R = r + dr[i];
                        int C = c + dc[i];
                        if(R < 0 || C < 0 || R >= matrix.length || C >= matrix[0].length) {
                            continue;
                        }
                        // we have found a path... so go to that cell
                        if(matrix[R][C] > matrix[r][c]) {
                            max = Math.max(max, recur(R, C, matrix));
                        }
                    }
                    return max + 1;
                }
             
        
        
             Step III (Using DP: From TLE to Accepted): 
            
        - Now for every state [i, j] we might be recomputing them again and again during recursion.
 - This can result into TLE.
 - So what we will do is, after computing value of [i, j] we will store it into dp array mapping them by [i, j].
 - This step is shown in code below.
 
            Full Code (Java):
            
                class Solution {
                    int[] dr, dc, dp[];
                    public int longestIncreasingPath(int[][] matrix) {
                        dr = new int[] {1, 0, -1, 0};
                        dc = new int[] {0, 1, 0, -1};
                        
                        int n = matrix.length;
                        int m = matrix[0].length;
                        dp = new int[n][m];
                        
                        int max = 1;
                        for(int i = 0; i < n; i++) {
                            for(int j = 0; j < m; j++) {
                                max = Math.max(max, recur(i, j, matrix));
                            }
                        }
                        
                        return max;
                    }
                    
                    int recur(int r, int c, int[][] matrix) {
                        int max = 0;
                        // checking if the result for this subproblem is stored
                        // if yes we will return this value directly without further computation
                        if(dp[r][c] != 0) {
                            return dp[r][c];
                        }
                        for(int i = 0; i < 4; i++) {
                            int R = r + dr[i];
                            int C = c + dc[i];
                            if(R < 0 || C < 0 || R >= matrix.length || C >= matrix[0].length) {
                                continue;
                            }
                            if(matrix[R][C] > matrix[r][c]) {
                                max = Math.max(max, recur(R, C, matrix));
                            }
                        }
                        // storing the value of current subproblem in DP
                        dp[r][c] = max + 1;
                        return max + 1;
                    }
                }
             
        
    
Comments
Post a Comment
If you have any queries or need solution for any problem, please let me know.