Longest Increasing Continuous subsequence II

Give you an integer matrix (with row size n, column size m),find the longest increasing continuous subsequence in this matrix. (The definition of the longest increasing continuous subsequence here can start at any row or column and go up/down/right/left any direction).

Given a matrix:

[
  [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]
]

return 25

Lintcode上的一题,这题很有意思,要求是二维矩阵里面连续递增的子序列.连续是在矩阵的四个方向都可以.如示例是在矩阵的外圈绕了一圈.和最长递增子序列类似.我们定义这里的dp状态为dp[i][j]为以i,j为结尾的最长连续递增子序列的长度.

所以可以得出递推关系:dp[i][j] = max{dp[i-1][j]+1(A[i-i][j]<A[i][j]), dp[i][j-1]+1(A[i][j-1] < A[i][j]), dp[i+1][j]+1(A[i+1][j]<A[i][j]), dp[i][j+1]+1(A[i][j+1]<A[i][j])即在上下左右四个方向上进行递推.

初始化dp[i][j] = 1, 即当前字符.

最终状态max(dp[i][j]).既然dp[i][j]定义的是以某个位置结束的最长递增子序列的长度.则矩阵中一共有m*n个元素,则遍历完这些元素就可以.复杂度为O(n^2),这题因为初始化长度就是1,也有可能此处的最长长度就是1,所以用一个dp矩阵同时发挥visited矩阵很困难.需要另外设立一个flag矩阵.

代码如下:

class Solution:
    # @param {int[][]} A an integer matrix
    # @return {int}  an integer
    def longestIncreasingContinuousSubsequenceII(self, A):
        if not A or not A[0]:
            return 0
        n = len(A)
        m = len(A[0])
        flag = [[False]*m for i in xrange(n)]
        dp = [[1]*m for i in xrange(n)]
        maxVal = 1
        for i in xrange(n):
            for j in xrange(m):
                maxVal = max(maxVal, self.search(A, flag, dp, i, j))
        return maxVal
    def search(self, A, flag, dp, x, y):
        if flag[x][y]:
            return dp[x][y]
        dx = [0, 0, -1, 1]
        dy = [1, -1, 0, 0]

        for i in xrange(4):
            if 0 <= x + dx[i] < len(A) and 0 <= y+dy[i] < len(A[0]) and A[x][y] > A[x+dx[i]][y+dy[i]]:
                dp[x][y] = max(dp[x][y], self.search(A, flag, dp, x+dx[i], y+dy[i])+1)
        flag[x][y] = True
        return dp[x][y]
时间: 2024-10-07 14:19:02

Longest Increasing Continuous subsequence II的相关文章

LintCode &quot;Longest Increasing Continuous subsequence II&quot; !!

DFS + Memorized Search (DP) class Solution { int dfs(int i, int j, int row, int col, vector<vector<int>>& A, vector<vector<int>>& dp) { if(dp[i][j] != 0) return dp[i][j]; if (i > 0 && A[i-1][j] > A[i][j]) { dp

[LintCode] Longest Increasing Continuous Subsequence

Give an integer array,find the longest increasing continuous subsequence in this array. An increasing continuous subsequence: Can be from right to left or from left to right. Indices of the integers in the subsequence should be continuous. Example Fo

[LintCode] Longest Increasing Continuous Subsequence 最长连续递增子序列

Give an integer array,find the longest increasing continuous subsequence in this array. An increasing continuous subsequence: Can be from right to left or from left to right. Indices of the integers in the subsequence should be continuous. Notice O(n

Longest Increasing Continuous Subsequence

Give an integer array,find the longest increasing continuous subsequence in this array. An increasing continuous subsequence: Can be from right to left or from left to right. Indices of the integers in the subsequence should be continuous. Notice O(n

lintcode 容易题:Longest Increasing Continuous subsequence 最长上升连续子序列

题目: 最长上升连续子序列 给定一个整数数组(下标从 0 到 n-1, n 表示整个数组的规模),请找出该数组中的最长上升连续子序列.(最长上升连续子序列可以定义为从右到左或从左到右的序列.) 样例 给定 [5, 4, 2, 1, 3], 其最长上升连续子序列(LICS)为 [5, 4, 2, 1], 返回 4. 给定 [5, 1, 2, 3, 4], 其最长上升连续子序列(LICS)为 [1, 2, 3, 4], 返回 4. 解题: 这个直接找就可以了,最长的升序,和最长的降序,再求最大值,时

lintcode-easy-Longest Increasing Continuous Subsequence

Give an integer array,find the longest increasing continuous subsequence in this array. An increasing continuous subsequence: Can be from right to left or from left to right. Indices of the integers in the subsequence should be continuous. Example Fo

Longest Increasing Common Subsequence (LICS)

最长上升公共子序列(Longest Increasing Common Subsequence,LICS)也是经典DP问题,是LCS与LIS的混合. Problem 求数列 a[1..n], b[1..m]的LICS的长度, a[], b[]数组的元素均为正整数. Solution 考虑如何定义DP状态,定义DP状态就是定义所谓的最优子问题(optimal subproblem),而DP状态要能转移,就是所谓最优子问题要具有重叠子结构. 将DP状态定义为 DP[i][j]:a[1..i], b[

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 Exampl

LeetCode Number of Longest Increasing Subsequence

原题链接在这里:https://leetcode.com/problems/number-of-longest-increasing-subsequence/description/ 题目: Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: Input: [1,3,5,4,7] Output: 2 Explanation: The two longe