【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爬虫同时输出两个列表(zip函数)
简介:在做爬虫时,xpath返回的是列表格式,我们又需要将列表中的元素一一对应并存放至字典中,这是就可以用zip函数. zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组, ...
- [Golang学习笔记] 03 库源码文件
库源码文件:不能被直接运行的源码文件,它仅用于存放程序实体,这些程序实体可以被其他代码使用. 代码包声明的基本规则: 1. 同目录下的源码文件的代码包声明语句要一致.也就是说,它们要同属于一个代码包( ...
- DP_括号匹配序列问题
括号匹配问题 简单括号匹配问题是给出字符串,判断字符串中的括号是否匹配,此类问题核心解决方案就是利用栈的后进先出的特性,从左到右依次遍历字符串,遇左括号进栈,遇右括号将其与栈顶元素配对,若能配对,则栈 ...
- excel中CTRL+E的用法
偶然发现excel中CTRL+E有按照例子填充的功能. 结果如下
- 【SQLSERVER】服务挂起解决办法
一. 问题描述:某项SQLSERVER服务,运行状态为“正在挂起更改”,导致该服务无法使用,也不能启动.停止.重新启动. 二.解决方法 方法一:从任务管理器 → 进程 (勾上 显示所有用户进程) → ...
- PHP 行为测试工具 Codeception (介绍)
原文地址:https://phphub.org/topics/25 Codeception 简介 Codeception 简单来说, 分为以下几种测试 Acceptance Tests 验收测试 Fu ...
- 利用Anaconda进行python爬虫环境的配置-安装scrapy
1.下载Anaconda,下载地址:https://www.continuum.io/downloads 2.安装anaconda. 3.安装scrapy
- thinkphp5 开启多语言
一.配置点击打开链接1.开启语言包功能'lang_switch_on' => true,2.支持的语言列表'lang_list' => ['zh-cn','en-us'],二.语言定义(默 ...
- PHP:Iterator(迭代器)接口和生成器
迭代器 可在内部迭代自己的外部迭代器或类的接口.详情:http://php.net/manual/zh/class.iterator.php 接口摘要 Iterator extends Travers ...
- Egret入门(三)--创建HelloWorld项目(4.0-使用Egret Wing)
准备 编辑器: Egret Wing3(4.0.3) 需要下载安装 语言: TepyScript(JS的超集,参考手册http://bbs.egret.com/thread-1441-1-1.html ...