package com.fishercoder.solutions;

public class _329 {

    public static class Solution1 {
        final int[] dirs = new int[]{0, 1, 0, -1, 0};

        public int longestIncreasingPath(int[][] matrix) {
            if (matrix == null || matrix.length == 0) {
                return 0;
            }
            int max = 0;
            int[][] cache = new int[matrix.length][matrix[0].length];
            for (int i = 0; i < matrix.length; i++) {
                for (int j = 0; j < matrix[0].length; j++) {
                    int len = dfs(matrix, i, j, cache);
                    max = Math.max(len, max);
                }
            }
            return max;
        }

        int dfs(int[][] matrix, int row, int col, int[][] cache) {
            if (cache[row][col] != 0) {
                return cache[row][col];
            }
            int max = 1;
            for (int i = 0; i < 4; i++) {
                int nextRow = row + dirs[i];
                int nextCol = col + dirs[i + 1];
                if (nextRow < 0 || nextRow >= matrix.length || nextCol < 0 || nextCol >= matrix[0].length || matrix[nextRow][nextCol] <= matrix[row][col]) {
                    continue;
                }
                int len = 1 + dfs(matrix, nextRow, nextCol, cache);
                max = Math.max(max, len);
            }
            cache[row][col] = max;
            return max;
        }
    }

}

@@ -217,28 +217,14 @@
    i
-f (matrix == null %7C%7C
+nt m =
  mat
@@ -237,16 +237,9 @@
 ngth
- == 0) %7B
+;
 %0A   
@@ -251,48 +251,46 @@
     
-    return 0;%0A            %7D%0A
+int n = matrix%5B0%5D.length;%0A     
        
-     
 int 
@@ -339,38 +339,11 @@
 nt%5Bm
-atrix.length%5D%5Bmatrix%5B0%5D.length
+%5D%5Bn
 %5D;%0A 
@@ -374,28 +374,16 @@
 0; i %3C m
-atrix.length
 ; i++) %7B
@@ -419,32 +419,17 @@
  0; j %3C 
-matrix%5B0%5D.length
+n
 ; j++) %7B
@@ -820,9 +820,23 @@
 i %3C 
-4
+dirs.length - 1
 ; i+

refactor 329