当前位置:网站首页>Longest increasing subsequence problem (do you really know it)
Longest increasing subsequence problem (do you really know it)
2022-07-04 04:29:00 【A boy in the mountains】
Catalog
One . The longest increasing subsequence problem I
Two . The longest increasing subsequence problem II
3、 ... and . The longest increasing subsequence problem III
One . The longest increasing subsequence problem I
1. Corresponding Niuke website link
Longest ascending subsequence ( One )_ Niuke Tiba _ Cattle from (nowcoder.com)
2. Title Description :
3. Their thinking
1. First of all, we analyze the meaning of the topic : The longest increasing subsequence is disassembled : To increase , Or sequence , Not necessarily continuous , Want the longest .
2. We usually think about how to end in a certain position , In this question, we can think like this. We must take i What is the maximum length of the longest increasing subsequence at the end of the position? We do this at each position, so the answer must be in it
Let's say [5,7,1,9,4,6,2,8,3] For example :
First element 5: The incremental length can only be 1, Next, the second one 7, Than 5 Big , The length is 2
The third element 1: There is nothing bigger in front of it , Only for 1 , Fourth element 9, There is 5,7, Therefore, the length is 3 Here you find ,9 Than 7 Big ,7 The length of the forward formation is 2, that 9 You can connect to 7 Behind , Become a new sequence of length plus one .
I believe Lao tie should understand
4. The corresponding code :
class Solution { public: /** * The class name in the code 、 Method name 、 The parameter name has been specified , Do not modify , Return the value specified by the method directly * * The length of the longest strictly ascending subsequence of a given array . * @param arr int integer vector Given array * @return int integer */ int LIS(vector<int>& arr) { if (arr.empty()) return 0; // write code here int N = arr.size(); vector<int>dp(N, 1);// The length of the longest increasing subsequence at each position is at least 1 I am myself //dp The meaning of must be in i The maximum length of the longest increasing subsequence at the end of the position int maxLen = 1; for (int i = 1; i < N; i++) { for (int j = 0; j < i; j++) { if (arr[i] > arr[j]) { dp[i] = max(dp[i], dp[j] + 1); // The length of the add 1 } } maxLen = max(maxLen, dp[i]);// Update maximum length } return maxLen; } };
Two . The longest increasing subsequence problem II
1. Corresponding Niuke website link :
Longest ascending subsequence ( Two )_ Niuke Tiba _ Cattle from (nowcoder.com)
2. Title Description :
3. Their thinking :
1. Here we introduce end Array ,end[i] The value of is so far, and the length is i+1 The minimum end of
2. Every time I go end Look in the array for greater than or equal arr[i] Leftmost position
We see that the same length is 2 The subsequence ,[2,3] Just like [2,5] good . because [2,3] If there is 4 Words , form [2,3,4] Length is 3 了 , however [2,5] Because the conditions are not met , You can't form a team .
When we compose subsequences , Not only should this sequence be as long as possible , And let the rise in the subsequence be as slow as possible ,[2,3] Just like [2,5] Slow rise , In this way, there is a chance to splice longer ascending subsequences . We use an array to save the current longest ascending subsequence , This array is strictly incremented .
Because it is strictly increasing , The last value in the array nums[max] That's the maximum , If you come across another number next time n, It is better than num[max] Even bigger , So obviously , The length of this subsequence is +1, And the array n Add to the end of the array .[2,3,7,8,11,13,18] It is by far the longest ascending subsequence , Then if you encounter 19, perhaps 101, Because they are all larger than the maximum value in the array 18, So just add it to the end of the array , At the same time, the length of subsequence should be +1.19 and 101 The example of is easy to understand , But if the next number is 6 perhaps 12 Well ? Because the subsequence should rise as slowly as possible , So let's [2,5,7...] become [2,5,6...] More appropriate , Because the latter rises more slowly . Again , take [...8,11,13,18] become [...8,11,12,18] It also rises more slowly .
That is to say , Known ascending subsequence [i,i_1,i_2,....,i_n], Now we encounter a value in the process of continuing traversal i_k, This value is less than i_n Of , So the length of ascending subsequence remains unchanged . But we need to find a place , take i_k Replace an old value .Corresponding dynamic diagram :
4. The corresponding code :
class Solution { public: /** * The class name in the code 、 Method name 、 The parameter name has been specified , Do not modify , Return the value specified by the method directly * * The length of the longest strictly ascending subsequence of the array * @param a int integer vector Given array * @return int integer */ int LIS(vector<int>& arr) { // write code here if (arr.empty()) { return 0; } int N = arr.size(); vector<int>end(N); //end[i] It means that the length so far is i+1 The minimum end of end[0] = arr[0]; int L = 0; int maxLen = 1; int right = 0; // Used to control the end Range of array int R = right; for (int i = 1; i < N; i++) { L = 0; R = right; while (L <= R) {// Used to find greater than or equal to arr[i] The leftmost position int mid = (L + R) >> 1; if (arr[i] > end[mid]) { L = mid + 1; } else { R = mid - 1; } } right = max(right, L); // Update right border maxLen = max(maxLen, L + 1); end[L] = arr[i]; } return maxLen; } };
3、 ... and . The longest increasing subsequence problem III
1. Corresponding letecode link :
Longest ascending subsequence ( 3、 ... and )_ Niuke Tiba _ Cattle from (nowcoder.com)
2. Title Description :
3. Their thinking
1. This question is only an upgraded version of the previous question. We only need to define a variable record Just tell me where the end of the longest increasing subsequence is , Then traverse in turn
4. The corresponding code :
class Solution { public: /** * retrun the longest increasing subsequence * @param arr int integer vector the array * @return int integer vector */ vector<int> LIS(vector<int>& arr) { // write code here if (arr.empty()) { return {}; } int N = arr.size(); vector<int>dp(N, 1); vector<int>end(N); int maxLen = 1; int maxIndex = 0; end[0] = arr[0]; int right = 0; int L = 0; int R = right; for (int i = 1; i < N; i++) { L = 0; R = right; while (L <= R) { int mid = (L + R) >> 1; if (arr[i] > end[mid]) { L = mid + 1; } else { R = mid - 1; } } right = max(right, L); maxLen = max(maxLen, L + 1); end[L] = arr[i]; dp[i] = L + 1; if (dp[i] >= maxLen) {// Because it is necessary to minimize the subcriterion maxIndex = i; maxLen = dp[i]; } } vector<int>ans(maxLen);// Get the longest increasing subsequence for (int i = maxIndex; i >= 0; i--) { if (dp[i] == maxLen) { ans[--maxLen] = arr[i]; } } return ans; } };
Thinking questions : If you want to get all incremental subsequences ?
#include<iostream> #include<vector> using namespace std; vector<int> process(vector<int>& arr,vector<int>&dp ,int maxLen, int index) {// Get all the longest increasing subsequences vector<int>ans(maxLen); for (int i = index; i >= 0; i--) { if (dp[i] == maxLen) { ans[--maxLen] = arr[i]; } } cout << " Come in " << endl; return ans; } int main() { int N; cin >> N; vector<int>arr(N); for (int i = 0; i < N; i++) { cin >> arr[i]; } vector<int>dp(N, 1); vector<int>end(N); int maxLen = 1; int maxIndex = 0; end[0] = arr[0]; int right = 0; int L = 0; int R = right; for (int i = 1; i < N; i++) { L = 0; R = right; while (L <= R) { int mid = (L + R) >> 1; if (arr[i] > end[mid]) { L = mid + 1; } else { R = mid - 1; } } right = max(right, L); maxLen = max(maxLen, L + 1); end[L] = arr[i]; dp[i] = L + 1; if (dp[i] >= maxLen) { maxIndex = i; maxLen = dp[i]; } } vector<vector<int>>ans; for (int i = 0; i < N; i++) { if (dp[i] == maxLen) { ans.push_back(process(arr, dp, maxLen, i)); } } for (int i = 0; i < ans.size(); i++) { for (int j = 0; j < ans[0].size(); j++) { cout << ans[i][j] << " "; } cout << endl; } return 0; }
边栏推荐
- NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
- 博朗与Virgil Abloh于2021年为纪念博朗品牌100周年而联合打造的“功能性艺术”将在博物馆展出Abloh作品期间首次亮相
- 西部数据绿盘、蓝盘、黑盘、红盘和紫盘有什么区别
- Redis: order collection Zset type data operation command
- 2020 Bioinformatics | TransformerCPI
- Pytest basic self-study series (I)
- Main applications of TDK lambda power supply
- 【微服务|openfeign】feign的两种降级方式|Fallback|FallbackFactory
- NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
- What does software testing do? Find defects and improve the quality of software
猜你喜欢

A beautiful API document generation tool

I.MX6U-ALPHA开发板(C语言版本LED驱动实验)

仿《游戏鸟》源码 手游发号评测开服开测合集专区游戏下载网站模板

2021 RSC | Drug–target affinity prediction using graph neural network and contact maps

leetcode刷题:二叉树08(N叉树的最大深度)

Keysight n9320b RF spectrum analyzer solves tire pressure monitoring scheme

Flink learning 7: application structure
![[csrf-01] basic principle and attack and defense of Cross Site Request Forgery vulnerability](/img/46/cb5a10ffe3fcdffb7da68dbaef5b1f.png)
[csrf-01] basic principle and attack and defense of Cross Site Request Forgery vulnerability

FT2000+下LPC中断绑核使用说明

Graduation project
随机推荐
一个漂亮的API文档生成工具
Talking about what a high-quality little red book copy needs to have
Architecture practice camp - graduation project of module 9 of phase 6
Apple CMS imitation watermelon video atmospheric response video template source code
RHCSA 01 - 创建分区与文件系统
How to telecommute more efficiently | community essay solicitation
Select function variable column name in dplyr of R language
陪驾注意事项 这23点要注意!
浅谈JVM的那些事
A beautiful API document generation tool
FT2000+下LPC中断绑核使用说明
Modstartblog modern personal blog system v5.2.0 source code download
Distributed system: what, why, how
软件测试是干什么的 发现缺陷错误,提高软件的质量
【愚公系列】2022年7月 Go教学课程 002-Go语言环境安装
PPt 教程,如何在 PowerPoint 中将演示文稿另存为 PDF 文件?
2020 Bioinformatics | TransformerCPI
(指針)自己寫一個比較字符串大小的函數,功能與strcmp類似。
程序员远程办公喜忧参半| 社区征文
网络 - VXLAN








