• 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 }
  • 相关阅读:
    12个思维管理工具:标杆分析法、麦肯锡7步分析法、SMART原则....
    Capturing Audio in Android Q
    闭环思维
    如何下载Google Play商店的apk?如何安装分割的apk ?
    强制删除文件 或 文件夹
    ToDesk ---- 个人免费 极致流畅的远程协助软件
    无法显示内挂(非内嵌)字幕-
    MKVToolNix 一款Matroska(.mkv)格式编辑工具,可以将超过16条音轨/字幕封装到一个.mkv文件中去
    Symantec Endpoint Protection(赛门铁克杀毒软件) 如何添加白名单避免被误删、误杀?
    二维码识别工具
  • 原文地址:https://www.cnblogs.com/joycelee/p/5348982.html
Copyright © 2020-2023  润新知