Snake Sequence

Problem

You are given a grid of numbers. A snake sequence is made up of adjacent numbers such that for each number, the number on the right or the number below it is +1 or -1 its value. For example,

1 3 2 6 8
-9 7 1 -1 2
1 5 0 1 9

In this grid, (3, 2, 1, 0, 1) is a snake sequence. Given a grid, find the longest snake sequences and their lengths 

(so there can be multiple snake sequences with the maximum length).

Solution

 1 public int findSnakeSequence(int[][] matrix) {
 2 //        List<List<Integer>> res = ArrayList<ArrayList<Integer>>();
 3     if(matrix == null || matrix.length == 0) {
 4         return -1;
 5     }
 6     int m = matrix.length;
 7     int n = matrix[0].length;
 8
 9     int[][] dp = new int[m][n];
10     int globalMax = 0;
11
12     //initialize
13     dp[0][0] = 1;
14     for(int i=1; i<n; i++) {
15         if(Math.abs(matrix[0][i] - matrix[0][i-1]) == 1)
16             dp[0][i] = dp[0][i-1] + 1;
17         else
18             dp[0][i] = 1;
19     }
20
21     for(int i=1; i<m; i++) {
22         if(Math.abs(matrix[i][0] - matrix[i-1][0]) == 1)
23             dp[i][0] = dp[i-1][0] + 1;
24         else
25             dp[i][0] = 1;
26     }
27
28     for(int i=1; i<m; i++) {
29         for(int j=1; j<n; j++) {
30             int max = 1;
31             if(Math.abs(matrix[i][j] - matrix[i][j-1]) == 1)
32                 max = dp[i][j-1] + 1;
33             if(Math.abs(matrix[i][j] - matrix[i-1][j]) == 1)
34                 max = Math.max(max, dp[i-1][j] + 1);
35             dp[i][j] = max;
36             globalMax = Math.max(max, globalMax);
37         }
38     }
39
40     for (int i = 0; i < m; i++) {
41     for (int j = 0; j < n; j++) {
42         System.out.print(dp[i][j] + " ");
43     }
44     System.out.print("\n");
45     }
46
47     return globalMax;
48
49 }
时间: 2024-08-29 00:08:17

Snake Sequence的相关文章

Epic - Snake Sequence

You are given a grid of numbers. A snakes equence is made up of adjacent numbers such that for each number, the number on the right or the number below it is +1 or -1 its value. For example, 1 3 2 6 8 -9 7 1 -1 2 1 5 0 1 9 In this grid, (3, 2, 1, 0,

Cipe Coding Summary Part1

1. Colorful Number:A numbercan be broken into different sub-sequence parts. Suppose a number 3245 can bebroken into parts like 3 2 4 5 32 24 45 324 245. And this number is a colorfulnumber, since product of every digit of a sub-sequence are different

LeetCode OJ - Longest Consecutive Sequence

这道题中要求时间复杂度为O(n),首先我们可以知道的是,如果先对数组排序再计算其最长连续序列的时间复杂度是O(nlogn),所以不能用排序的方法.我一开始想是不是应该用动态规划来解,发现其并不符合动态规划的特征.最后采用类似于LRU_Cache中出现的数据结构(集快速查询和顺序遍历两大优点于一身)来解决问题.具体来说其数据结构是HashMap<Integer,LNode>,key是数组中的元素,所有连续的元素可以通过LNode的next指针相连起来. 总体思路是,顺序遍历输入的数组元素,对每个

1005 Number Sequence

Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Given A, B, and n, you are to calculate the value of f(n). Input The input consists of multiple test cases. Each test case co

HDU 5783 Divide the Sequence(数列划分)

HDU 5783 Divide the Sequence(数列划分) Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)   Problem Description - 题目描述 Alice has a sequence A, She wants to split A into as much as possible continuous subsequences, satisfy

HDU 3397 Sequence operation(线段树)

HDU 3397 Sequence operation 题目链接 题意:给定一个01序列,有5种操作 0 a b [a.b]区间置为0 1 a b [a,b]区间置为1 2 a b [a,b]区间0变成1,1变成0 3 a b 查询[a,b]区间1的个数 4 a b 查询[a,b]区间连续1最长的长度 思路:线段树线段合并.须要两个延迟标记一个置为01,一个翻转,然后因为4操作,须要记录左边最长0.1.右边最长0.1,区间最长0.1,然后区间合并去搞就可以 代码: #include <cstdi

HDU 3998 Sequence (最长递增子序列+最大流SAP,拆点法)经典

Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1666    Accepted Submission(s): 614 Problem Description There is a sequence X (i.e. x[1], x[2], ..., x[n]). We define increasing subsequ

HDOJ 5147 Sequence II 树状数组

树状数组: 维护每一个数前面比它小的数的个数,和这个数后面比他大的数的个数 再枚举每个位置组合一下 Sequence II Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 121    Accepted Submission(s): 58 Problem Description Long long ago, there is a seq

Acdream 1427 Nice Sequence

Nice Sequence Time Limit: 12000/6000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) Problem Description Let us consider the sequence a1, a2,..., an of non-negative integer numbers. Denote as ci,j the number of occurrences of the number i a