【leetcode】300.Longest Increasing Subsequence
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的更多相关文章
- 【LeetCode】300. Longest Increasing Subsequence 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【刷题-LeetCode】300. Longest Increasing Subsequence
Longest Increasing Subsequence Given an unsorted array of integers, find the length of longest incre ...
- 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)
[LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...
- 【Lintcode】076.Longest Increasing Subsequence
题目: Given a sequence of integers, find the longest increasing subsequence (LIS). You code should ret ...
- 【LeetCode】516. Longest Palindromic Subsequence 最长回文子序列
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 代码 刷题心得 日期 题目地址:https://le ...
- 【LeetCode】594. Longest Harmonious Subsequence 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计次数 日期 题目地址:https://leetc ...
- 【LeetCode】329. Longest Increasing Path in a Matrix 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/longest- ...
- 【LeetCode】521. Longest Uncommon Subsequence I 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【leetcode】521. Longest Uncommon Subsequence I
problem 521. Longest Uncommon Subsequence I 最长非共同子序列之一 题意: 两个字符串的情况很少,如果两个字符串相等,那么一定没有非共同子序列,反之,如果两个 ...
随机推荐
- 从零开始的Python学习Episode 15——正则表达式
正则表达式 正则表达式(或 RE)是一种小型的.高度专业化的编程语言,(在Python中)它内嵌在Python中,并通过 re 模块实现,所以使用时要导入re模块.正则表达式模式被编译成一系列的字节码 ...
- Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符
/* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...
- 使用maven将GitHub上项目打包作为依赖添加
Clone GitHub项目 git clone https://github.com/LeeKemp/UserAgentParser 将项目打成jar包,安装到maven仓库中 D:\Github ...
- PMP考试通过
经过3个月的努力,终于在10月8号,过完国庆假期,得知考试通过.虽然没有得到5A,只有4A,心也算落下了.备考的过程中,通过学习小组讨论,互相交流,辅导. 自己也对学习的知识加深印象.总结一下整个学习 ...
- 20155239《Java程序设计》实验一(Java开发环境的熟悉)实验报告
实验内容及步骤 使用JDK编译.运行简单的java程序 2.使用IDEA编辑.编译.运行.调试Java程序 (一)使用JDK编译.运行简单的java程序 命令行下的程序开发 先建立一个文件夹命名为Co ...
- U盘装系统流程_U启动
步骤 (已有装机U盘的前提下,没有则参考U启动制作U盘启动盘详细教程) (这里默认已设置了相关开机设置,若没设置则参考怎么用u盘装win7系统(推荐看)中步骤11-13) 按开机键后,不断按F12(有 ...
- 20155322 2016-2017-2 《Java面向对象程序设计》第十二周课堂练习之Arrays和String单元测试
20155322 2016-2017-2 <Java面向对象程序设计>第十二周课堂练习之Arrays和String单元测试 练习目地 在IDEA中以TDD的方式对String类和Array ...
- echarts 去掉最外部边框
在option中,插入一下代码即可: grid: {show:'true',borderWidth:'0'}, 插入代码前: 插入代码后:
- WPF中。。DataGrid 实现时间控件和下拉框控件
DatePicker 和新的 DataGrid 行 用户与 DataGrid 中日期列的交互给我造成了很大的麻烦. 我通过将一个 Data Source 对象拖动到 WPF 窗口上,创建了一个 Dat ...
- PostgreSQL的checkpoint能否并行
对于此问题,在社区进行了提问,并得到了一些大牛的解答: http://postgresql.1045698.n5.nabble.com/Can-checkpoint-creation-be-paral ...