Longest increasing continuous subsequence II

Source: Internet
Author: User

Give you a integer matrix (with row size n, column size m), find the longest increasing continuous subsequence in this mat Rix. (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]]

Return25

Lintcode on the topic, the problem is very interesting, the requirement is a two-dimensional matrix inside the continuous increment of the sub-sequence. Continuous is available in four directions of the Matrix. The example is a circle around the outer ring of the matrix. is similar to the longest increment subsequence. We define the DP state here as DP[I][J] to the length of the longest continuous increment subsequence ending with i,j.

So you can get a recursive relationship: 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]) is recursive in the upper and lower left and right four directions.

Initialize DP[I][J] = 1, which is the current character.

Final state Max (Dp[i][j]). Since dp[i][j] defines the length of the longest increment subsequence that ends at a certain location. If there are m*n elements in the matrix, then the elements can be traversed. Complexity is O (n^2), because the initialization length is 1, it is possible that the longest length here is 1, so it is difficult to use a DP matrix to play the visited matrix simultaneously. A flag matrix needs to be set up separately.

The code is as follows:

classSolution:#@param {int[][]} A an integer matrix    #@return {int} an integer    defLongestincreasingcontinuoussubsequenceii (Self, A):if  notAor  notA[0]:return0 N=Len (A) m=Len (a[0]) flag= [[False]*m forIinchxrange (N)] DP= [[1]*m forIinchxrange (n)] Maxval= 1 forIinchxrange (n): forJinchxrange (m): Maxval=Max (Maxval, Self.search (A, Flag, DP, I, j))returnMaxvaldefSearch (self, A, flag, DP, X, y):ifFlag[x][y]:returnDp[x][y] DX= [0, 0,-1, 1] Dy= [1,-1, 0, 0] forIinchXrange (4):            if0 <= x + dx[i] < Len (A) and0 <= Y+dy[i] < Len (a[0]) andA[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]=TruereturnDp[x][y]

Longest increasing continuous subsequence II

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.