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

Example:

Input: [10,9,2,5,3,7,101,18]
Output: 4
Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4.

Note:

  • There may be more than one LIS combination, it is only necessary for you to return the length.
  • Your algorithm should run in O(n2) complexity.

Follow up: Could you improve it to O(n log n) time complexity?

使用dp,时间复杂度为O(n2)

 public int lengthOfLIS(int[] nums) {//dp my
if(null==nums||0==nums.length){
return 0;
}
int max= 1;
int[] re = new int[nums.length];//存放当前位置的最大长度
re[0]=1;
for(int i=1;i<nums.length;i++){
re[i]=0;
for(int j=i-1;j>=0;j--){
if(nums[j]<nums[i]&&re[i]<re[j]){//从当前位置往前,第一个比nums[i]小的值
re[i] = re[j];
}
}
re[i]++;
if(re[i]>max){
max =re[i];
}
}
return max;
}

利用二分,时间复杂度为O(nlogn)

public int lengthOfLIS(int[] nums) {//二分 mytip
if(null==nums||0==nums.length){
return 0;
}
List<Integer> re = new ArrayList<>();//
re.add(nums[0]);
int index = 0;
for(int i=1;i<nums.length;i++){
if(nums[i]>re.get(re.size()-1)){//如果大于最后一个元素,直接插入
re.add(nums[i]);
}
else{
index = bs(0,re.size()-1,re,nums[i]);//二分找到第一个不大于nusm[i]的数的下标,然后替换为当前数
re.set(index,nums[i]); }
}
return re.size();//数组长度为最大值
}
private int bs(int left,int right,List<Integer> list,int num){
while(left<=right){
if(left >= right){
return left;
}
else{
int mid = left + (right - left)/2;
if(list.get(mid)<num){
left = mid+1;
}
else{
right =mid;
}
}
}
return left;
}

LeetCode-300.Longst 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

    一.题目链接:https://leetcode.com/problems/longest-increasing-subsequence/ 二.题目大意: 给定一个没有排序的数组,要求从该数组中找到一个 ...

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

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

  9. 【leetcode】300.Longest Increasing Subsequence

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

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

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

随机推荐

  1. react 生命周期钩子里不要写逻辑,否则不生效

    react 生命周期钩子里不要写逻辑,否则不生效,要把逻辑写在函数里,然后在钩子里调用函数,否则会出现问题.

  2. [Tensorflow] RNN - 04. Work with CNN for Text Classification

    Ref: Combining CNN and RNN for spoken language identification Ref: Convolutional Methods for Text [1 ...

  3. sklearn数据预处理

    一.standardization 之所以标准化的原因是,如果数据集中的某个特征的取值不服从标准的正太分布,则性能就会变得很差 ①函数scale提供了快速和简单的方法在单个数组形式的数据集上来执行标准 ...

  4. django 1.11.1 连接MySQL

    一.定义数据库 settings.py搜索   DATABASES 参考路径:   D:\Python27\Lib\site-packages\django\bin\app\app\settings. ...

  5. 《Thinkphp5使用Socket服务》 入门篇

    上车啦!!! 今天来说一下thinkphp5.0下使用wokerman的socket服务. 安装: composer的安装方法,Windows下直接下个composer的应用程序,双击安装,环境变量同 ...

  6. 低耦合高内聚 - 不要把所有东西都放在 vuex中

    我就举一个例子.比如,我想看电视,是否需要遥控器??请认真思考这个问题. 看似电视与“我”已经解耦了.然而,我需要通过遥控器去看电视,我的目的是看电视,但是我却需要依赖遥控器这个中间件.这就变相地将“ ...

  7. Linux多进程和多线程的一次gdb调试实例

    转载https://typecodes.com/cseries/multilprocessthreadgdb.html 1 原文整理 默认设置下,在调试多进程程序时gdb只会调试主进程.gdb7以上的 ...

  8. .NET Core 2.0 单元测试中初识 IOptionsMonitor<T>

    在针对下面设置 CookieAuthenticationOptions 的扩展方法写单元测试时遇到了问题. public static IServiceCollection AddCnblogsAut ...

  9. 关于form表单排版的技巧

    //此处说明一个对于排版表单的方法将form表单嵌套在table里,因为table是格式化的,所以就不用再手动排版了源码如下 <from action="" method=& ...

  10. Hive之变量和属性

    首先看一下hive cli工具对于变量的定义规定的几项功能: $ bin/hive -h usage: hive -d,--define <key=value>          Vari ...