LeetCode-300.Longst Increasing Subsequence
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 is4
.
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的更多相关文章
- [LeetCode] 300. Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...
- leetcode@ [300] Longest Increasing Subsequence (记忆化搜索)
https://leetcode.com/problems/longest-increasing-subsequence/ Given an unsorted array of integers, f ...
- Leetcode 300 Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- [leetcode]300. Longest Increasing Subsequence最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...
- [leetcode] 300. Longest Increasing Subsequence (Medium)
题意: 求最长增长的子序列的长度. 思路: 利用DP存取以i作为最大点的子序列长度. Runtime: 20 ms, faster than 35.21% of C++ online submissi ...
- LeetCode 300. Longest Increasing Subsequence最长上升子序列 (C++/Java)
题目: Given an unsorted array of integers, find the length of longest increasing subsequence. Example: ...
- LeetCode——300. Longest Increasing Subsequence
一.题目链接:https://leetcode.com/problems/longest-increasing-subsequence/ 二.题目大意: 给定一个没有排序的数组,要求从该数组中找到一个 ...
- 【LeetCode】300. Longest Increasing Subsequence 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【leetcode】300.Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- 【刷题-LeetCode】300. Longest Increasing Subsequence
Longest Increasing Subsequence Given an unsorted array of integers, find the length of longest incre ...
随机推荐
- 第四百零一节,Django+Xadmin打造上线标准的在线教育平台—生产环境部署virtualenv虚拟环境安装,与Python虚拟环境批量安装模块
第四百零一节,Django+Xadmin打造上线标准的在线教育平台—生产环境部署virtualenv虚拟环境安装,与Python虚拟环境批量安装模块 virtualenv简介 1.安装virtuale ...
- GDC2017 把“现实的天空”在游戏内再现【Forza Horizon 3】的天空表现
原文链接 http://game.watch.impress.co.jp/docs/news/1047800.html 完全表现出现实世界中各种偶然而不可预料的风景! [Forza Horiz ...
- iOS开发之--宏定义与const的区别及使用方法
宏定义的常见用法: 定义一段代码,或指定字符串抽成宏. const(常量): 当有字符串常量的时候,苹果推荐我们使用const,苹果经常把常用的字符串定义成const 宏定义与const的区别: 编译 ...
- puppt服务资源管理
1.服务资源的特性 controllable 提供变量控制 enableable 可以启动 停止服务 refreshable 可以重启服务 2.可用参数: ensure ...
- Linux Platform驱动模型(三) _platform+cdev
平台总线是一种实现设备信息与驱动方法相分离的方法,利用这种方法,我们可以写出一个更像样一点的字符设备驱动,即使用cdev作为接口,平台总线作为分离方式: xjkeydrv_init():模块加载函数 ...
- 【SpringCloud错误】错误记录
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates ...
- Elasticsearch 学习之 节点重启
ElasticSearch集群的高可用和自平衡方案会在节点挂掉(重启)后自动在别的结点上复制该结点的分片,这将导致了大量的IO和网络开销.如果离开的节点重新加入集群,elasticsearch为了对数 ...
- [ASP.NET MVC]视图是如何呈现的
为了搞清楚ASP.NET MVC的请求过程,我们计划从结果追踪到源头.使用VS2012创建一个空白的ASP.NET MVC项目 然后创建一个HelloController 创建一个HelloView. ...
- Oracle数据库入门——sql语句和函数详解
一.oracle常用数据类型 一. 数据定义语言(ddl) 数据定义语言ddl(data definition language)用于改变数据库结构,包括创建.更改和删除数据库对象. 用于操纵表结构 ...
- 使用 tabindex 改变Tab 键顺序
使用 tabindex原文 https://developers.google.cn/web/fundamentals/accessibility/focus/using-tabindex 在表单上使 ...