当前位置:网站首页>76 · 最长上升子序列
76 · 最长上升子序列
2022-04-22 06:16:00 【yinhua405】
描述
给定一个整数序列,找到最长上升子序列(LIS),返回LIS的长度。
最长上升子序列的定义:
最长上升子序列问题是在一个无序的给定序列中找到一个尽可能长的由低到高排列的子序列,这种子序列不一定是连续的或者唯一的。
https://en.wikipedia.org/wiki/Longest_increasing_subsequence
样例
样例 1:
输入:
nums = [5,4,1,2,3]
输出:
3
解释:
LIS 是 [1,2,3]
样例 2:
输入:
nums = [4,2,4,5,3,7]
输出:
4
解释:
LIS 是 [2,4,5,7]
挑战
要求时间复杂度为O(n2)O(n^2)O(n2) 或者 O(nlogn)O(nlogn)O(nlogn)
int longestIncreasingSubsequence(vector<int> &nums) {
// write your code here
int size = nums.size();
if (size <= 1)
{
return size;
}
vector<int> dp(size, 1);
int max = 1;
//dp[0] = 1;
for (int i = 1; i<size; i++)
{
for (int j = i - 1; j >= 0; j--)
{
if (nums[i] == nums[j])
{
dp[i] = dp[i]>dp[j] ? dp[i] : dp[j];
}
else if (nums[i] > nums[j])
{
dp[i] = dp[i]>dp[j] + 1 ? dp[i] : dp[j] + 1;
if (dp[i] > max)
{
max = dp[i];
}
}
}
}
return max;
}
版权声明
本文为[yinhua405]所创,转载请带上原文链接,感谢
https://blog.csdn.net/yinhua405/article/details/122613685
边栏推荐
- [number theory] congruence (4): univariate linear congruence equations (elimination of two, Chinese remainder theorem)
- Quartus II prevents signals from being integrated
- 微信浏览器无法长期保存cookie
- C语言 | 指针
- Some mechanisms of synchronized lock optimization (lock upgrade)
- SQL Server quick start
- MacOS installs redis and sets the service self start
- [Opt 31-67] Problem axi_interconnect RTL报错
- Anaconda安装与使用
- Jenkins deployment PM2
猜你喜欢

MySQL learning notes

二叉树链式结构操作LeetCode+牛客(详解)
![ERROR: [Hsi 55-1545] ,无法正常生成fsbl,Unable to read in MSS file,Failed to closesw system.mss](/img/4e/34e2820ff8579007b20b33b27d8f1d.png)
ERROR: [Hsi 55-1545] ,无法正常生成fsbl,Unable to read in MSS file,Failed to closesw system.mss

Define an abstract role class with name, age, gender, hobbies and other member variables. It is required to hide all variables as far as possible (private if possible), and then read and write each va

Write a method Sanjiao (a, B, c) to judge whether the three parameters can form a triangle. If not, throw an exception illegalargumentexception and display the exception information a, B, C "cannot fo

ASP. Net daily development notes ----- IIS server supports downloading APK

指针 结构体 const 小结

Find a notepad file by yourself, find the data material by yourself, and count the times of three keywords or sentence entries in the whole text.

Anaconda安装与使用

Byte Summer Internship - 20220304
随机推荐
. net learning notes - about Net core (1) [. NETCORE's project structure, five ways to transfer values to pages, and the use of log4net and NLog]
机械设计知识点规划
Points for attention in Modelsim simulation acceleration
LaTex中添加作者照片和作者简介
[number theory] congruence (7): fast power, fast power of matrix
【数论】素数(五):梅森素数(Lucas_Lehmer判定法)
Redis进阶
LaTex中插入图片报错Unknown graphics extension: .1.jpg. }
【数论】同余(四):一元线性同余方程组(两两相消、中国剩余定理)
Raspberry Pi 4b
1. Compile the following three modules of student information management system: and detect the implementation. 1. Add student information 4. Query student information 5. Query all student information
【数论】欧拉函数(基本性质、递推法、公式法、线性筛法)
Define an abstract role class with name, age, gender, hobbies and other member variables. It is required to hide all variables as far as possible (private if possible), and then read and write each va
SQL server stored procedure development notes - piecemeal problems and operations on operation files
树+二叉树 详解 详解 +Top-k问题
Define the class shape as the parent class, and define the method to calculate the perimeter and area in the class; (2) Define the shape subclass circle, with radius attribute and constant PI, and ove
Define a student class 1 to get the student's name: get_ Name() return type: STR 2 get student's age: get_ Age() return type: int 3 returns the highest score among the three subjects. get_ course()
Jenkins deployment PM2
快排与归并排序
Write a method Sanjiao (a, B, c) to judge whether the three parameters can form a triangle. If not, throw an exception illegalargumentexception and display the exception information a, B, C "cannot fo