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 ...
随机推荐
- [Object Tracking] Overview of algorithms for Object Tracking
From: https://www.zhihu.com/question/26493945 可以载入史册的知乎贴 目标跟踪之NIUBILITY的相关滤波 - 专注于分享目标跟踪中非常高效快速的相关滤波 ...
- HandlerSocket介绍
HandlerSocket的原理 HandlerSocket的应用场景: MySQL自身的局限性,很多站点都采用了MySQL+Memcached的经典架构,甚至一些网站放弃MySQL而采用NoSQL产 ...
- Unity3D Shader 高斯模糊
//Shader Shader "Hidden/GaussianBlur" { Properties { _MainTex ("Texture", 2D) = ...
- VS 2013 未找到与约束contractname Microsoft.VisualStudio.Utilities.IContentTypeRegistryService......匹配的导出
错误信息: 点击解决方案中的文件,就会提示这个错误.最近给vs2013安装了python的插件,安装了vs2017的python,可能是这两个导致了这个错误. 解决方案: 删除 C:\Users\ji ...
- ssm文件配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- 一、SQL定义变量
http://blog.csdn.net/changwei07080/article/details/7561602 在SQL我们使用declare定义局部变量,同时可以使用set和select 对变 ...
- {MySQL的库、表的详细操作}一 库操作 二 表操作 三 行操作
MySQL的库.表的详细操作 MySQL数据库 本节目录 一 库操作 二 表操作 三 行操作 一 库操作 1.创建数据库 1.1 语法 CREATE DATABASE 数据库名 charset utf ...
- Orchard之Module开发
一:生成新项目 首先,要启动 Code Generation,参考<Orchard之生成新模板>. 其次,进入命令行,输入: codegen module Tminji.Requireme ...
- H. GSS and Simple Math Problem 高精度乘法模板
链接:https://www.nowcoder.com/acm/contest/104/G来源:牛客网 题目描述 Given n positive integers , your task is to ...
- gfs故障恢复
GlusterFS更换故障Brick tanzhenchao关注0人评论3918人阅读2017-03-21 09:53:18 1 前言 笔者公司内有一套GlusterFS分布式存储,最近数据分区的 ...