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 ...
随机推荐
- 【C++札记】指针数组和数组指针
指针数组: 存储指针的数组,数组找那个的每个一元素都是指针 例: int* p1[4],p2[0]是一个指向int类型的指针 char* p2[4],p1[0]是一个指向char类型的指针 数组指针: ...
- websocket 函数
函数名 描述 socket_accept() 接受一个Socket连接 socket_bind() 把socket绑定在一个IP地址和端口上 socket_clear_error() 清除socket ...
- java中反射的使用
结合demo理解反射: import java.lang.reflect.*; /** * 反射使用 **/ public class ReflectDemo{ public static void ...
- 作业调度框架Quartz.NET-现学现用-02-任务监听
原文:作业调度框架Quartz.NET-现学现用-02-任务监听 前言 任务调度系统并不是完美的,它会出现任务执行失败的情况.如果你需要处理任务失败后的逻辑,希望这篇笔记可以为你提供些帮助. Quar ...
- Web.sitemap网站导航
全文注释: Web.sitemap导航XML文件,站点地图,功能实现菜单 1.xml的版本1.0 和编码utf-8 2.Url链接 Title显示的标题 Description描述(ToolTip) ...
- 转 C# 使用openssl
//先用大整数来生成一个1024bit的密钥对 RSA rsa = new RSA(); BigNumber number = OpenSSL.Core.Random.Next(10, 10, 1); ...
- Python接口自动化基础---环境准备
安装requests模块 pip install requests request帮助文档查看 import requests print(help(requests)) Help on packag ...
- jenkins配置Webhook-gitlab
1.Jenkins 安装完成以后,首先我们在Jenkins中需要安装一下,Gitlab Hook Plugin,GitLab Plugin,Gitlab Authentication plugin插件 ...
- Android Jetpack组件 - ViewModel,LiveData使用以及原理
本文涉及的源码版本如下: com.android.support:appcompat-v7:27.1.1 android.arch.lifecycle:extensions:1.1.1 android ...
- centos 7.6 修改vim配色方案
cd ~ vim .vimrc colorscheme desert