日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

lintcode 最长上升连续子序列 II(二维最长上升连续序列)

發布時間:2025/3/8 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 lintcode 最长上升连续子序列 II(二维最长上升连续序列) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接:http://www.lintcode.com/zh-cn/problem/longest-increasing-continuous-subsequence-ii/

最長上升連續子序列 II ?

  給定一個整數矩陣(其中,有 n 行, m 列),請找出矩陣中的最長上升連續子序列。(最長上升連續子序列可從任意行或任意列開始,向上/下/左/右任意方向移動)。

樣例

給定一個矩陣

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

返回?25

思路:記憶化搜索 + dp

  設Lics(num)表示以num開頭的最長上升子連續序列的長度, 則Lics(A[x][y]) = max(Lics(A[x-1][y]), Lics(A[x][y-1]),?Lics(x+1,y),?Lics(x, y+1))+1;

#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<vector> #include<deque> #include<map> using namespace std;class Solution { public:/*** @param A an integer matrix* @return an integer*/int n, m, maxL;int lics[1000][1000];int vis[1000][1000];int dir[4][2] = {{0, 1}, {1, 0}, {0,-1}, {-1,0}};int dfs(vector<vector<int>>& A, int x, int y){int maxLics = 0;vis[x][y] = 1;for(int i=0; i<4; ++i){int xx = x+dir[i][0];int yy = y+dir[i][1];if(xx<0 || yy<0 || xx>=n || yy>=m) continue;if(A[x][y] >= A[xx][yy]) continue;if(!vis[xx][yy])maxLics = max(maxLics, dfs(A, xx, yy));elsemaxLics = max(maxLics, lics[xx][yy]);}lics[x][y] = maxLics+1;if(maxL < lics[x][y]) maxL = lics[x][y];return lics[x][y];} int longestIncreasingContinuousSubsequenceII(vector<vector<int>>& A) {n = A.size();if(n == 0) return 0;m = A[0].size();memset(lics, 0, sizeof(lics));memset(vis, 0, sizeof(vis));maxL = 0;for(int i=0; i<n; ++i)for(int j=0; j<m; ++j)if(!vis[i][j])dfs(A, i, j);return maxL;} }; /* 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 */

?

轉載于:https://www.cnblogs.com/hujunzheng/p/4986801.html

總結

以上是生活随笔為你收集整理的lintcode 最长上升连续子序列 II(二维最长上升连续序列)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。