Saturday, October 15, 2016

Longest Increasing Path in a Matrix

Given an integer matrix, find the length of the longest increasing path.
From each cell, you can either move to four directions: left, right, up or down. You may NOT move diagonally or move outside of the boundary (i.e. wrap-around is not allowed).
Example 1:
nums = [
  [9,9,4],
  [6,6,8],
  [2,1,1]
]
Return 4
The longest increasing path is [1, 2, 6, 9].
Example 2:
nums = [
  [3,4,5],
  [3,2,6],
  [2,2,1]
]
Return 4
The longest increasing path is [3, 4, 5, 6]. Moving diagonally is not allowed.
The question is marked hard, but the idea is not very difficult. We still use DFS, but the trick here is we track a traversed matrix, which stores the longest path current coordinate can have if it is traversed previously. Update the traversed matrix every time we finish traversing all neighbors of a point.

Note here we first check all neighbors of a point, then update the point with the longest path we can find.


public int longestIncreasingPath(int[][] matrix) {
        if (matrix == null || matrix.length == 0 || matrix[0] == null || matrix[0].length == 0)
            return 0;
        int max = 0;
        int[][] traversed = new int[matrix.length][matrix[0].length];
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[0].length; j++) {
                max = Math.max(max, traverse(matrix, i, j, traversed, -1));
            }
        }
        return max;
        
    }
    private int traverse(int[][] matrix, int x, int y, int[][]traversed, int former) {
        if (x < 0 || x >= matrix.length || y < 0 || y >= matrix[0].length || matrix[x][y] <= former)
            return 0;
        if (traversed[x][y] != 0)
            return traversed[x][y];
        int right = traverse(matrix, x + 1, y, traversed, matrix[x][y]);
        int left = traverse(matrix, x - 1, y, traversed, matrix[x][y]);
        int up = traverse(matrix, x, y - 1, traversed, matrix[x][y]);
        int down = traverse(matrix, x, y + 1, traversed, matrix[x][y]);
        int maxCurr = Math.max(Math.max(left, right), Math.max(up, down));
        traversed[x][y] = maxCurr + 1;
        return traversed[x][y];
    }


1 comment:

  1. The development of artificial intelligence (AI) has propelled more programming architects, information scientists, and different experts to investigate the plausibility of a vocation in machine learning. Notwithstanding, a few newcomers will in general spotlight a lot on hypothesis and insufficient on commonsense application. IEEE final year projects on machine learning In case you will succeed, you have to begin building machine learning projects in the near future.

    Projects assist you with improving your applied ML skills rapidly while allowing you to investigate an intriguing point. Furthermore, you can include projects into your portfolio, making it simpler to get a vocation, discover cool profession openings, and Final Year Project Centers in Chennai even arrange a more significant compensation.


    Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account.

    ReplyDelete