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 ...
随机推荐
- 协议——SPI
SPI(Serial Peripheral interface)是由摩托罗拉公司定义的一种串行外围设备接口,是一种高速.全双工.同步的通信总线,只需要四根信号线即可,节约引脚,同时有利于PCB的布局. ...
- TypeScript之函数
1.函数声明 与javascript一样,ts的函数声明也分为两种:函数声明,函数表达式 1)函数声明: function fn(age:number):string{ return `age is ...
- gitlab-runner 的 executors 之 docker
gitlab-runner 的 executors 之 docker GitLab Runner 实现了许多执行程序,可用于在不同的场景中运行构建.所有执行程序分别为: SSH Shell Paral ...
- Consul 的安装与基本使用
什么是 Consul Consul是一种服务网格解决方案,提供具有服务发现,配置和分段功能的全功能控制平面.这些功能中的每一个都可以根据需要单独使用,也可以一起使用以构建全服务网格.Consul需 ...
- Unity项目 - 坦克大战3D TankBattle
目录 游戏原型 项目演示 绘图资源 代码实现 技术探讨 参考来源 游戏原型 游戏玩法:在有界的战场上,玩家将驾驶坦克,代表绿色阵营,与你的队友一起击溃红蓝阵营的敌人,在这场三方大战中夺得胜利! 操作指 ...
- java 正则和连接json
前面已经写了不少关于C# 怎么使用正则,有兴趣,可以翻译成java代码. 以图片为例子: import java.util.regex.Matcher; import java.util.regex. ...
- 记一次在 Get 请求参数为 Null 值的折腾
先说主要原因,是因为一个 NgZerro 的 Select 组件,需要显示 placeHolder 文字,初始值为 null,然后直接绑定到查询参数中,传输到后端导致 BadRequest,参数解析失 ...
- 【洛谷 P2226】 [HNOI2001]遥控赛车比赛(最短路)
题目链接 首先拆点,把每个点拆成4个点,表示到达这个点的时候赛车的朝向. 然后考虑连边. 相邻同向并且都是可以走的点直接连边权1的边. 至于怎么转向,只需在每个点\(i\)向每个方向一直拓展直到不能走 ...
- IOS - 总结下swift使用GCD 多线程(二)GCD和DispatchQueue
1.前言 iOS中处理多核并发的技术有两种分别是:`Grand Central Dispatch`(以下简称`GCD`)和`NSOperationQueue`框架.iOS开发的老司机们在程序开发中处 ...
- vue-cli 3.0 安装
一.安装vue-cli 脚手架命令 npm install -g vue-cli 适用于vue-cli 2.0 npm install -g @vue/cli 适用于vue-cli 3.0 卸载命 ...