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. [Installing Metasploit Framework on CentOS_RHEL 6]在CentOS_RHEL 6上安装Metasploit的框架【翻译】

    [Installing Metasploit Framework on CentOS_RHEL 6]在CentOS_RHEL 6上安装Metasploit的框架[翻译] 标记声明:蓝色汉子为翻译上段英 ...

  2. [Full-stack] 异步即时通信 - Async

    故事背景 socket.io, node.js, koa为首的一些通信框架和后端技术点. 之后有必要过一遍<NodeJS 设计模式>. 基础概念 一.短轮询.长轮询(comet).长连接( ...

  3. 学 shell (1/5)

    假设这是某脚本 x.sh 的内容,使用 sh x.sh arg1 来执行该脚本 #!/bin/bashcd `dirname $0`/..source scripts/status.shstart $ ...

  4. 【typecho】解决使用分隔符 <!--more-->标签后首页文字下面出现一段空白

    使用typecho 搭建了一个站点,输出摘要时候.使用了   <!--more-->  分隔符,然后首页文章出现了一大片空白,审查元素发现.多了好多 <br> 标签 解决办法: ...

  5. 【问题集】redis.clients.jedis.exceptions.JedisDataException: ERR value is not an integer or out of range

    redis.clients.jedis.exceptions.JedisDataException: ERR value is not an integer or out of range incrm ...

  6. 【CF717G】Underfail 费用流

    [CF717G]Underfail 题意:赌城拉斯维起司的赌场最近推出了一种新式赌法.它的玩法是由庄家(Joker)设局,赌徒只需要交付一定数额的赌资即可入局.具体地,Joker将给出一个长度为 $n ...

  7. exec vs sp_executesql

    1.exec  vs  sp_executesql 1.1 说到exec了解SQLServer的朋友第一反应应该是它用来执行存储过程,对的这是其一,另一个作用是执行一个动态批处理.总结下:a.执行一个 ...

  8. get_or_create函数

    get_or_create函数比较好用. 如果查询到就返回,如果没查询到就向数据库加入新的对象. e.g. size = Size.objects.get_or_create(sizeName=siz ...

  9. FastDFS数据迁移

    参考:https://blog.csdn.net/frvxh/article/details/56293502 FastDFS安装配置参考:https://www.cnblogs.com/minseo ...

  10. Kernel parameter requirements ( Linux DB2)

    Kernel parameter requirements ( Linux DB2) https://www.ibm.com/support/knowledgecenter/SSEPGG_9.7.0/ ...