Longest Continuance Increasing sub-sequence in matrix

一个矩阵,求最长连续的序列长度

[1 2 3

4 5 6

7 8 9]

我的解法时用dfs遍历矩阵,如果便利过的元素就标记为false,不再遍历。

邻居 就是上下左右

e.g.

1 2 3

6 5 4 -> 7

7 9 8

===================

5 7 9

1 2 3 -> 3

4 6 8

===================

9 8 7

4 5 6 ->9

3 2 1

题目一. 只能上下左右,走到边界处止步

题目二. 每一行/列都是一个循环,走到边界处譬如第 0 列,可以继续走到另一边界,譬如 n-1

 1 /*
 2  * Graph DFS
 3  *
 4  * Find the Longest Continuance Increasing sub-sequence in 2d matrix
 5  * You could go up, down, left, right in the matrix
 6  *
 7  */
 8
 9 public class LIS2d {
10     public int findLongest(int[][] matrix) {
11         int max = 0;
12         int m = matrix.length, n = matrix[0].length;
13         boolean[][] visit = new boolean[m][n];
14         for (int i = 0; i < m; i++) {
15             for (int j = 0; j < n; j++) {
16                 int len = helper(matrix, visit, i, j, 1);
17                 max = max > len ? max : len;
18             }
19         }
20         return max;
21     }
22     public int helper(int[][] matrix, boolean[][] visit, int i, int j, int step) {
23         visit[i][j] = true;
24         int max = step;
25         int m = matrix.length, n = matrix[0].length;
26 //        if (i > 0 && matrix[i-1][j] == matrix[i][j]+1 && !visit[i-1][j]) {
27 //            int len = helper(matrix, visit, i-1, j, step+1);
28 //            max = max > len ? max : len;
29 //        }
30 //        if (i < m-1 && matrix[i+1][j] == matrix[i][j]+1 && !visit[i+1][j]) {
31 //            int len = helper(matrix, visit, i+1, j, step+1);
32 //            max = max > len ? max : len;
33 //        }
34 //        if (j > 0 && matrix[i][j-1] == matrix[i][j]+1 && !visit[i][j-1]) {
35 //            int len = helper(matrix, visit, i, j-1, step+1);
36 //            max = max > len ? max : len;
37 //        }
38 //        if (j < n-1 && matrix[i][j+1] == matrix[i][j]+1 && !visit[i][j+1]) {
39 //            int len = helper(matrix, visit, i, j+1, step+1);
40 //            max = max > len ? max : len;
41 //        }
42         int r = (i-1+m) % m;
43         int c = j;
44         if (matrix[r][c] == matrix[i][j]+1 && !visit[r][c]) {
45             int len = helper(matrix, visit, r, c, step+1);
46             max = max > len ? max : len;
47         }
48         r = (i+1+m) % m;
49         c = j;
50         if (matrix[r][c] == matrix[i][j]+1 && !visit[r][c]) {
51             int len = helper(matrix, visit, r, c, step+1);
52             max = max > len ? max : len;
53         }
54         r = i;
55         c = (j-1+n) % n;
56         if (matrix[r][c] == matrix[i][j]+1 && !visit[r][c]) {
57             int len = helper(matrix, visit, r, c, step+1);
58             max = max > len ? max : len;
59         }
60         r = i;
61         c = (j+1+n) % n;
62         if (matrix[r][c] == matrix[i][j]+1 && !visit[r][c]) {
63             int len = helper(matrix, visit, r, c, step+1);
64             max = max > len ? max : len;
65         }
66         visit[i][j] = false;
67         return max;
68     }
69     public static void main(String[] args) {
70         int[][] matrix = {{5, 6, 7},
71                           {4, 10, 9},
72                           {3, 2, 8}};
73         LIS2d lis = new LIS2d();
74         System.out.println(lis.findLongest(matrix));
75     }
76 }
时间: 2024-08-08 13:51:44

Longest Continuance Increasing sub-sequence in matrix的相关文章

[LeetCode]Longest Increasing Path in a Matrix

题目: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 bounda

329. Longest Increasing Path in a Matrix

/* * 329. Longest Increasing Path in a Matrix * 2016-7-9 by Mingyang * 其实这种类型的题目没有想象的那么难,你只需要好好的按照dfs的概念一点一点的走就好了 * 记住,每个dfs不光是void的,也可以返回一个长度 */ public static final int[][] dirs = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } }; public int longestIncre

[LeetCode][JavaScript]Longest Increasing Path in a Matrix

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

Leetcode之深度优先搜索(DFS)专题-DFS+记忆化 329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix)

Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix) 深度优先搜索的解题详细介绍,点击 给定一个整数矩阵,找出最长递增路径的长度. 对于每个单元格,你可以往上,下,左,右四个方向移动. 你不能在对角线方向上移动或移动到边界外(即不允许环绕). 示例 1: 输入: nums = [ [9,9,4], [6,6,8], [2,1,1] ] 输出: 4 解释: 最长递增路径为 [1, 2, 6, 9].

Longest Continuous Increasing Subsequence II

Description Given an integer matrix. Find the longest increasing continuous subsequence in this matrix and return the length of it. The longest increasing continuous subsequence here can start at any position and go up/down/left/right. Example Exampl

674. Longest Continuous Increasing Subsequence

Given an unsorted array of integers, find the length of longest continuous increasing subsequence. Example 1: Input: [1,3,5,4,7] Output: 3 Explanation: The longest continuous increasing subsequence is [1,3,5], its length is 3. Even though [1,3,5,7] i

674. Longest Continuous Increasing Subsequence 最长连续增长的子序列

Given an unsorted array of integers, find the length of longest continuous increasing subsequence. Example 1: Input: [1,3,5,4,7] Output: 3 Explanation: The longest continuous increasing subsequence is [1,3,5], its length is 3. Even though [1,3,5,7] i

[LeetCode] Longest Continuous Increasing Subsequence 最长连续递增序列

Given an unsorted array of integers, find the length of longest continuous increasing subsequence. Example 1: Input: [1,3,5,4,7] Output: 3 Explanation: The longest continuous increasing subsequence is [1,3,5], its length is 3. Even though [1,3,5,7] i

674. Longest Continuous Increasing Subsequence最长连续递增子数组

[抄题]: Given an unsorted array of integers, find the length of longest continuous increasing subsequence (subarray). Example 1: Input: [1,3,5,4,7] Output: 3 Explanation: The longest continuous increasing subsequence is [1,3,5], its length is 3. Even t