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
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);
}
}
}
}
}
Longest Continuous Increasing Subsequence II的更多相关文章
- LeetCode 674. Longest Continuous Increasing Subsequence (最长连续递增序列)
Given an unsorted array of integers, find the length of longest continuous increasing subsequence. E ...
- [LeetCode] Longest Continuous Increasing Subsequence 最长连续递增序列
Given an unsorted array of integers, find the length of longest continuous increasing subsequence. E ...
- [Swift]LeetCode674. 最长连续递增序列 | Longest Continuous Increasing Subsequence
Given an unsorted array of integers, find the length of longest continuous increasing subsequence (s ...
- [Leetcode]674. Longest Continuous Increasing Subsequence
Given an unsorted array of integers, find the length of longest continuous increasing subsequence. E ...
- [LeetCode&Python] Problem 674. Longest Continuous Increasing Subsequence
Given an unsorted array of integers, find the length of longest continuousincreasing subsequence (su ...
- leetcode300. Longest Increasing Subsequence 最长递增子序列 、674. Longest Continuous Increasing Subsequence
Longest Increasing Subsequence 最长递增子序列 子序列不是数组中连续的数. dp表达的意思是以i结尾的最长子序列,而不是前i个数字的最长子序列. 初始化是dp所有的都为1 ...
- 674. Longest Continuous Increasing Subsequence最长连续递增子数组
[抄题]: Given an unsorted array of integers, find the length of longest continuous increasing subseque ...
- LeetCode Longest Continuous Increasing Subsequence
原题链接在这里:https://leetcode.com/problems/longest-continuous-increasing-subsequence/description/ 题目: Giv ...
- 674. Longest Continuous Increasing Subsequence@python
Given an unsorted array of integers, find the length of longest continuous increasing subsequence (s ...
随机推荐
- Java反射桥接方法解析
在阅读mybaits源码的反射模块时,看到了如下的一段代码: /** * 添加 Method 数组到 uniqueMethods * @param uniqueMethods * @param met ...
- Java开发笔记(一百三十九)JavaFX的输入框
循着Swing的旧例,JavaFX仍然提供了三种文本输入框,分别是单行输入框TextField.密码输入框PasswordField.多行输入框TextArea.这些输入框都由抽象类TextInput ...
- web页面元素定位
所有web网页中有8种元素定位方式 靠单一的特征找元素:6种(id,class_name,tag_name,name,link_text(2))组合各种特征和关系来找元素:2种(xpath,css) ...
- WAMP集成环境虚拟路径修改
只需要改httpd.conf这一个文件就好了. 1.单击右下角wamp图标如下图打开httpd.conf,或者从文件夹打开httpd.conf.
- linux centos7开机自动启动程序实现
1存放脚本位置 /etc/init.d/ServerManagerCLI.sh 该脚本是自己新建的内容参看2 增加执行权限 chmod +x /etc/rc.d/init.d/ServerManage ...
- vim实用操作指南
一.查看文件编码 :set fileencoding :set fileencoding=utf-8 通过该命令可直接将文件修改为UTF-8编码 强制以指定编码打开,vim中 :e ++enc=utf ...
- promise实现
目录 promise实现 Promise 是 ES6 新增的语法,解决了回调地狱的问题. 可以把 Promise 看成一个状态机.初始是 pending 状态,可以通过函数 resolve 和 rej ...
- [CodeChef-ANUDTQ] Dynamic Trees and Queries
类似维护括号序列,给每个点建两个点,然后所有操作都能轻松支持了.注意sum和lastans是long long. #include<cstdio> #include<algorith ...
- NetCore踩坑记1、 一块网卡引发的血案
公司的项目架构演进,我们也趁机尝试迁移到netcore,系列随笔讲记录我们的踩坑和填坑记录. HttpClient不行? 这是我们第一次尝试netcore 简要介绍环境 netcore2.2+aspn ...
- 扩展JS
//JS的扩展方法: 1 定义类静态方法扩展 2 定义类对象方法扩展 var aClass = function(){} //1 定义这个类的静态方法 aC ...