一.题目链接:https://leetcode.com/problems/longest-increasing-subsequence/

二.题目大意:

  给定一个没有排序的数组,要求从该数组中找到一个最长的递增子序列,并返回其长度。(递增子序列可以是不连续的)

注:子序列和子字符串或者连续子集的不同之处在于,子序列不需要是原序列上连续的值。

三.题解:

  寻找最长递增子序列问题 (LIS),是一道比较经典的问题,该问题的解法也比较多,此处我只用了两种方法实现,所以不本无也主要陈述这两种算法。

方法1:

利用动态规划解决 (最容易想到的算法),时间复杂度为O(N^2),空间复杂度为O(N),基本思想如下:

(1)定义一个数组dp,其中dp[i]表示nums数组中,以nums[i]为结尾的最长递增子序列的长度。dp数组的所有元素的初值为1。

(2)接下来对dp数组进行维护更新,从nums的首元素nums[0]开始遍历,对所有排在nums[i]元素之前的元素,如果$ dp[j]+1 > dp[i] $,则$dp[i] = dp[j] + 1$,所以状态转方程为

$$ dp[i] = \begin{cases} \max\{dp[i],dp[j]+ 1\} ,& {j < i} \\ dp[i] , & {j ≥ i} \end{cases} $$

(3)在dp数组的更新过程中,记录其最大的元素值,最终结果即该值。

代码如下:

class Solution
{
public:
int lengthOfLIS(vector<int>& nums)
{
int len = nums.size();
int rs = 0;
if(len == 0)
return 0;
vector<int>dp(len,1);//初始化dp数组
for(int i = 0; i < len; i++)
{
for(int j = 0; j < i; j++)
if(nums[i] > nums[j])
dp[i] = max(dp[i],dp[j] + 1);//更新当前位置元素的dp
rs = max(rs,dp[i]);//每次更新dp[i]时,记录最大值
}
return rs;
} };

方法2:

利用二分查找的思想,时间复杂度为O(NlogN),空间复杂度为O(N)。该算法的想如下:

(1)定义一个tail数组,其中tail[i]表示数组nums中长度为i+1的递增子序列中末尾元素最小的那个子序列的末尾元素,例如:

 len =    :      [], [], [], []   => tails[] =
len = : [, ], [, ] => tails[] =
len = : [, , ] => tails[] =

(2)很显然,tail数组一定是个递增的有序数组,tail数组的初始大小为1,初始元素我们定义为nums[0] (也可以定义为nums中其他值,但最好是nums[0],因为数组的长度可能为1),对于其更新过程,详细如下:

1.遍历数组nums中的每一元素nums[i],如果nums[i] < tail[0]的话,令tail[0] = nums[i];

2.如果 nums[i] 大于taIl数组的租后一个元素的话 (由于tail数组时递增的,相当于大于tail数组中所有的元素),此时将tail数组的size加1,并令nums[i]为tail数组中新的尾元素。

3.如果nums[i]不满足1和2中的情况的话,那就利用二分法从tail数组中找到处第一个大于nums[i]的元素的位置 (假设为K),则更新tail数组,令tail[K] = nums[i]。

最终tail数组的大小,就是最长递增子序列的长度 (根据tail数组所表示的意思可知),但是tail数组中的元素构成的序列并不一定是一个最长递增子序列,只是数组大小等于LIS的长度而已。

具体的代码如下:

class Solution
{
public:
int lengthOfLIS(vector<int>& nums)
{
int len = nums.size();
if(len == 0)
return 0;
vector<int>tail;
tail.push_back(nums[0]);//初始化tail数组,一开始长度为1
for(int i = 0; i < len; i++)
{
if(nums[i] < tail[0]) //更新tail数组首元素
tail[0] = nums[i];
else if(nums[i] > tail.back())//更新tail数组尾元素
tail.push_back(nums[i]);
else//更新tail1数组其他位置的元素,即找到第一个大于等于nums[i]的元素
{
int left = 0, right = tail.size() - 1;
while(left <= right)//利用二分法找到第一个大于等于nums[i]的元素的位置,并进行更新
{
int mid =left + (right - left) / 2;//防止溢出
if(tail[mid] >= nums[i])
right = mid - 1;
else
left = mid + 1;
}
tail[left] = nums[i]; //left即第一个大于等于nums[i]的元素的位置
}
}
return tail.size();//最终tail数组的大小即所求的结果
} };

注:此处的tail数组实质为一个动态数组,所以用vector来实现最好不过了。

利用二分法查找数组中第一个大于等于key的问题实质为二分查找的变种。

参考:https://leetcode.com/problems/longest-increasing-subsequence/discuss/74824/JavaPython-Binary-search-O(nlogn)-time-with-explanation

  

LeetCode——300. Longest Increasing Subsequence的更多相关文章

  1. [LeetCode] 300. Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...

  2. leetcode@ [300] Longest Increasing Subsequence (记忆化搜索)

    https://leetcode.com/problems/longest-increasing-subsequence/ Given an unsorted array of integers, f ...

  3. Leetcode 300 Longest Increasing Subsequence

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  4. [leetcode]300. Longest Increasing Subsequence最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...

  5. [leetcode] 300. Longest Increasing Subsequence (Medium)

    题意: 求最长增长的子序列的长度. 思路: 利用DP存取以i作为最大点的子序列长度. Runtime: 20 ms, faster than 35.21% of C++ online submissi ...

  6. LeetCode 300. Longest Increasing Subsequence最长上升子序列 (C++/Java)

    题目: Given an unsorted array of integers, find the length of longest increasing subsequence. Example: ...

  7. 【LeetCode】300. Longest Increasing Subsequence 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. 【leetcode】300.Longest Increasing Subsequence

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  9. 【刷题-LeetCode】300. Longest Increasing Subsequence

    Longest Increasing Subsequence Given an unsorted array of integers, find the length of longest incre ...

随机推荐

  1. python自动化测试入门篇-jemter连接mysql数据库

    jmeter对数据库的操作主要包括以下几个步骤:1.导入mysqlde jdbc的jar包:2.创建数据库连接配置:3.线程组添加jdbc request;4.启动按钮,添加查看结果树 一.准备好驱动 ...

  2. Java中Annotation用法

    其他还可以参考的地址 https://www.cnblogs.com/skywang12345/p/3344137.html Annotation Annotation其实是代码里的特殊标记,这些标记 ...

  3. Servlet中的过滤器Filter

    链web.xml中元素执行的顺序listener->filter->struts拦截器->servlet. 1.过滤器的概念 Java中的Filter 并不是一个标准的Servlet ...

  4. javax.net.ssl.SSLException: Certificate doesn't match any of the subject alternative names

    问题:在使用 org.apache.http.*下的 CloseableHttpClient 发送https请求时报了以上错误 解决方案一:使用java.net.HttpURLConnection i ...

  5. Java toBinaryString()函数探究及Math.abs(-2147483648)=-2147483648原理探究

    toBinaryString()函数 public class Customer { public static void main(String[] args) { int m=-8; System ...

  6. leetcode python 009

    ##懒得自己做 ##  验证回文数字int0=63435435print(int(str(int0)[::-1])==int)

  7. mysql varchar存储最大

    utf-8的汉字 3个字节,varchar()括号中的数字是可存储的最大字符数,但是总和不超过65535个字节,这是行的size限制的,除以3差不多21800多,算上其他列等信息,如果用最大的话设置2 ...

  8. dos脚本2

    一.简单批处理内部命令简介  1.Echo 命令  打开回显或关闭请求回显功能,或显示消息.如果没有任何参数,echo 命令将显示当 前回显设置.  语法  echo [{on off}] [mess ...

  9. 莫烦tensorflow(6)-tensorboard

    import tensorflow as tfimport numpy as np def add_layer(inputs,in_size,out_size,n_layer,activation_f ...

  10. windows 重装系统

    转载:https://blog.csdn.net/qq_41137650/article/details/80035921 老毛桃 大白菜都可以 电脑系统安装步骤我选的是用U盘做的系统   如果本计系 ...