[LintCode] Longest Increasing Continuous subsequence II
public int longestIncreasingContinuousSubsequenceII(int[][] A) { // Write your code here if(A == null || A.length == 0 || A[0].length == 0) return 0; int res = 0; n = A.length; m = A[0].length; int[][] dp = new int[n][m]; for(int i = 0 ; i < n; i++) for(int j = 0 ; j < m; j++) res = Math.max(res,dfs(A,dp,i,j)); return res; } public int dfs(int[][] A, int[][] dp, int x, int y) { if(dp[x][y] != 0) return dp[x][y]; for (int k = 0; k < 4; k++) { int nx = x + dx[k]; int ny = y + dy[k]; if (0<=nx && nx < A.length && 0 <= ny && ny < A[0].length && A[x][y] < A[nx][ny]){ dp[x][y] = Math.max(dp[x][y], dfs(A,dp,nx,ny)); } } dp[x][y] ++; return dp[x][y]; }public int longestIncreasingContinuousSubsequenceII(int[][] A) { // Write your code here if(A == null || A.length == 0 || A[0].length == 0) return 0; int res = 0; n = A.length; m = A[0].length; int[][] dp = new int[n][m]; for(int i = 0 ; i < n; i++) for(int j = 0 ; j < m; j++) res = Math.max(res,dfs(A,dp,i,j)); return res; } public int dfs(int[][] A, int[][] dp, int x, int y) { if(dp[x][y] != 0) return dp[x][y]; for (int k = 0; k < 4; k++) { int nx = x + dx[k]; int ny = y + dy[k]; if (0<=nx && nx < A.length && 0 <= ny && ny < A[0].length && A[x][y] < A[nx][ny]){ dp[x][y] = Math.max(dp[x][y], dfs(A,dp,nx,ny)); } } dp[x][y] ++; return dp[x][y]; }
Leave A Comment