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

For example,
Given [10, 9, 2, 5, 3, 7, 101, 18],
The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that 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?

Tip:给定一个无序数组,求出数组中最长的递增子序列的长度。(我原来错误的以为求连续的最长递增子序列长度,但本文并没有要求子序列连续。)

实例化一个与给定数组nums相同长度的数组min来存储递增子序列,并另min[0]=nums[0]。

遍历nums数组,如果后一个元素大于当前元素,min[len++]=nums[i].

否则就从min数组中找到最后一个小于当前元素的位置,并插入到min数组中,(最后一个小于当前元素的后面)。

findPosition函数用来找到插入位置,时间复杂度为O(logn)。

lengthofLIS()遍历整个数组时间复杂度为O(n)

所以整体复杂度为O(nlogn)

package medium;

public class L300LongestIncreasingSubsequence {
public int lengthOfLIS(int[] nums) {
if (nums == null || nums.length <= 0)
return 0;
int len = 0;
int[] min = new int[nums.length];
min[len++] = nums[0];
for (int i = 1; i < nums.length; i++) {
if (nums[i] > min[len - 1]) {
min[len++] = nums[i];
} else {
int position = findPosition(min, 0, len - 1, nums[i]);
System.out.println(position);
min[position] = nums[i];
}
}
return len;
} private int findPosition(int[] min, int low, int high, int k) {
// 在min【】中找到k可以换的位置
while (low <= high) {
int mid = low + (high - low) / 2;
if (min[mid] == k) {
return mid;
} else if (min[mid] > k) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return low;
} public static void main(String[] args) {
L300LongestIncreasingSubsequence l300 = new L300LongestIncreasingSubsequence();
int[] nums = { 10, 9, 2, 5, 3, 7, 101, 18 };
int[] nums1 = { 1, 3, 2 };
int len = l300.lengthOfLIS(nums1);
System.out.println(len);
}
}

【leetcode】300.Longest Increasing Subsequence的更多相关文章

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

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

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

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

  3. 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)

    [LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...

  4. 【Lintcode】076.Longest Increasing Subsequence

    题目: Given a sequence of integers, find the longest increasing subsequence (LIS). You code should ret ...

  5. 【LeetCode】516. Longest Palindromic Subsequence 最长回文子序列

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 代码 刷题心得 日期 题目地址:https://le ...

  6. 【LeetCode】594. Longest Harmonious Subsequence 解题报告(Python & C++)

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

  7. 【LeetCode】329. Longest Increasing Path in a Matrix 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/longest- ...

  8. 【LeetCode】521. Longest Uncommon Subsequence I 解题报告(Python)

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

  9. 【leetcode】521. Longest Uncommon Subsequence I

    problem 521. Longest Uncommon Subsequence I 最长非共同子序列之一 题意: 两个字符串的情况很少,如果两个字符串相等,那么一定没有非共同子序列,反之,如果两个 ...

随机推荐

  1. golang channel 使用总结

    原文地址 不同于传统的多线程并发模型使用共享内存来实现线程间通信的方式,golang 的哲学是通过 channel 进行协程(goroutine)之间的通信来实现数据共享: Do not commun ...

  2. 微信小程序点击事件

    <---------------------------------------------------index文件夹:------------------------------------ ...

  3. PostgreSQL的checkpoint能否并行

    对于此问题,在社区进行了提问,并得到了一些大牛的解答: http://postgresql.1045698.n5.nabble.com/Can-checkpoint-creation-be-paral ...

  4. 记boost在gcc的一个库链接问题generic_category()

    报错大致如下: main.cpp:(.text+0x49): undefined reference to `boost::system::generic_category()'main.cpp:(. ...

  5. Maven学习(八)-----Maven依赖机制

    Maven依赖机制 在 Maven 依赖机制的帮助下自动下载所有必需的依赖库,并保持版本升级. 案例分析 让我们看一个案例研究,以了解它是如何工作的.假设你想使用 Log4j 作为项目的日志.这里你要 ...

  6. mybatis拦截器使用

    目录 mybatis 拦截器接口Interceptor spring boot + mybatis整合 创建自己的拦截器MyInterceptor @Intercepts注解 mybatis拦截器入门 ...

  7. Python接口测试实战3(上)- Python操作数据库

    如有任何学习问题,可以添加作者微信:lockingfree 课程目录 Python接口测试实战1(上)- 接口测试理论 Python接口测试实战1(下)- 接口测试工具的使用 Python接口测试实战 ...

  8. Unity —— 通过鼠标点击控制物体移动

    //ClickMove - - 通过鼠标点击控制物体移动 using System.Collections; using System.Collections.Generic; using Unity ...

  9. 设计模式C++实现(1)——策略(Strategy)模式

    目录 策略模式 应用案例 实现的关键 Talk is cheap,let's See The Code 设计思想 参考 策略模式 策略模式定义了一系列算法和行为(也就是策略),他们可以在运行时相互替换 ...

  10. openstack-r版(rocky)搭建基于centos7.4 的openstack swift对象存储服务 一

    openstack-r版(rocky)搭建基于centos7.4 的openstack swift对象存储服务 一 openstack-r版(rocky)搭建基于centos7.4 的openstac ...