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

Example 1:

Input:
[
[1, 2, 3, 4, 5],
[16,17,24,23,6],
[15,18,25,22,7],
[14,19,20,21,8],
[13,12,11,10,9]
]
Output: 25
Explanation: 1 -> 2 -> 3 -> 4 -> 5 -> ... -> 25 (Spiral from outside to inside.)

Example 2:

Input:
[
[1, 2],
[5, 3]
]
Output: 4
Explanation: 1 -> 2 -> 3 -> 5

Challenge

Assume that it is a N x M matrix. Solve this problem in O(NM) time and memory.

思路:

动态规划, 设定状态 f[i][j] 表示矩阵中坐标 (i, j) 的点开始的最长上升子序列

状态转移方程:

int dx[4] = {0, 1, -1, 0};
int dy[4] = {1, 0, 0, -1}; f[i][j] = max{ f[i + dx[k]][j + dy[k]] + 1 } k = 0, 1, 2, 3, matrix[i + dx[k]][j + dy[k]] > matrix[i][j]

这道题目可以向四个方向走, 所以推荐使用记忆化搜索(递归)的写法.

(当然, 也可以反过来设定: f[i][j] 表示走到 (i, j) 的最长上升子序列, 相应的状态转移方程做一点点改变即可)

public class Solution {
/**
* @param matrix: A 2D-array of integers
* @return: an integer
*/
int[][] dp;
int n, m; public int longestContinuousIncreasingSubsequence2(int[][] A) {
if (A.length == 0) {
return 0;
} n = A.length;
m = A[0].length;
int ans = 0;
dp = new int[n][m]; // dp[i][j] means the longest continuous increasing path from (i,j)
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
dp[i][j] = -1; // dp[i][j] has not been calculated yet
}
} for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
search(i, j, A);
ans = Math.max(ans, dp[i][j]);
}
} return ans;
} int[] dx = { 1, -1, 0, 0 };
int[] dy = { 0, 0, 1, -1 }; void search(int x, int y, int[][] A) {
if (dp[x][y] != -1) { // if dp[i][j] has been calculated, return directly
return;
} int nx, ny;
dp[x][y] = 1;
for (int i = 0; i < 4; ++i) {
nx = x + dx[i];
ny = y + dy[i];
if (nx >= 0 && nx < n && ny >= 0 && ny < m) {
if (A[nx][ny] > A[x][y]) {
search(nx, ny, A); // dp[nx][ny] must be calcuted
dp[x][y] = Math.max(dp[x][y], dp[nx][ny] + 1);
}
}
}
}
}

  

最新文章

  1. chrome拓展开发实战:页面脚本的拦截注入
  2. 日历插件FullCalendar应用:(一)数据展现
  3. seafile
  4. ATR与ATS
  5. 代码开光,Orz
  6. iconv gbk字符转utf8字符
  7. 3D图片采集与展示(SurfaceView 自适应 Camera, 录制视频, 抽取帧)
  8. 正则匹配去掉字符串中的html标签
  9. axios配合vue+webpack使用
  10. 【BZOJ2555】SubString(后缀自动机,Link-Cut Tree)
  11. C#DataTable添加列、C#指定位置添加列
  12. 【CH6803】导弹防御塔
  13. mysql: [Warning] Using a password on the command line interface can be insecure. ERROR 1045 (28000): Access denied for user &#39;root&#39;@&#39;localhost&#39; (using password: YES)
  14. FPGA的发展史及FPGA 的基础架构
  15. Django ModelForm 校验数据格式
  16. HihoCoder 1195 高斯消元&#183;一(高斯消元)
  17. Kylin 与 Spark SQL相比,有哪些差异和优势
  18. leetcode459
  19. Python自动化之session反解案例
  20. Shell中[]里面的条件判断

热门文章

  1. 如何在运行时更改JMeter的负载
  2. DP(动态规划)总结
  3. 【翻译】在GitHub上通过星级评估排名前10的最受欢迎的开源Delphi项目
  4. 对JAVA工程师绝对有用的Java学习资源清单
  5. 【scratch3.0教程】1.3 了解scratch界面内容
  6. vue 项目之后生成的 dist 文件该怎么在本地启动运行
  7. Computational biological hypothesis generation using &quot;-omics&quot; data
  8. serviceBehaviors_dataContractSerializer_maxItemsInObjectGraph 关键**Behavior
  9. Mongodb 学习笔记(一)
  10. React Native 开发豆瓣评分(四)集中管理 fetch 数据请求