题目描述

给定一个无序的整数数组,找到其中最长上升子序列的长度。

示例:

输入: [10,9,2,5,3,7,101,18]
输出: 4
解释: 最长的上升子序列是 [2,3,7,101],它的长度是 4。

说明:

  • 可能会有多种最长上升子序列的组合,你只需要输出对应的长度即可。
  • 你算法的时间复杂度应该为 O(n2) 。

进阶: 你能将算法的时间复杂度降低到 O(n log n) 吗?


解题思路

利用动态规划和二分查找解题。

遍历一遍数组,dp[]表示目前为止的上升子序列(不一定是最长),通过二分查找,找到当前元素在dp[]中的位置,如果为负值,则说明该元素比dp[]中所有元素都小,索引值要相应处理一下,然后原地替换;如果为正值,说明该元素比dp[]中所有元素都大,则加入尾部,顺便len++

Java 实现

public int lengthOfLIS (int[] nums) {
int[] dp = new int[nums.length];
int len = 0;
for (int num : nums) {
int i = Arrays.binarySearch(dp,0,len,num);
if (i < 0) i = -(i+1);
dp[i] = num;
if (i == len) len++;
}
return len;
}

心得体会

JDK 中数组二分查找的用法:

public static int binarySearch(int[] a,
int fromIndex,
int toIndex,
int key)

Searches a range of the specified array of ints for the specified value using the binary search algorithm. The range must be sorted (as by the sort(int[\], int, int) method) prior to making this call. If it is not sorted, the results are undefined. If the range contains multiple elements with the specified value, there is no guarantee which one will be found.

  • Parameters:

a - the array to be searched

fromIndex - the index of the first element (inclusive) to be searched

toIndex - the index of the last element (exclusive) to be searched

key - the value to be searched for

  • Returns:

index of the search key, if it is contained in the array within the specified range; otherwise, (-(*insertion point*) - 1). The insertion point is defined as the point at which the key would be inserted into the array: the index of the first element in the range greater than the key, or toIndex if all elements in the range are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.

  • Throws:

IllegalArgumentException - if fromIndex > toIndex

ArrayIndexOutOfBoundsException - if fromIndex < 0 or toIndex > a.length

  • Since:

1.6

【LeetCode】300-最长上升子序列的更多相关文章

  1. Java实现 LeetCode 300 最长上升子序列

    300. 最长上升子序列 给定一个无序的整数数组,找到其中最长上升子序列的长度. 示例: 输入: [10,9,2,5,3,7,101,18] 输出: 4 解释: 最长的上升子序列是 [2,3,7,10 ...

  2. leetcode 300最长上升子序列

    用递归DFS遍历所有组合肯定积分会超时,原因是有很多重复的操作,可以想象每次回溯后肯定会有重复操作.所以改用动态规划.建立一个vector<int>memo,初始化为1,memo[i]表示 ...

  3. Leetcode——300. 最长上升子序列

    题目描述:题目链接 给定一个无序的整数数组,找到其中最长上升子序列的长度. 示例: 输入: [10,9,2,5,3,7,101,18] 输出: 4 解释: 最长的上升子序列是 [2,3,7,101], ...

  4. Leetcode 300.最长上升子序列

    最长上升子序列 给定一个无序的整数数组,找到其中最长上升子序列的长度. 示例: 输入: [10,9,2,5,3,7,101,18] 输出: 4 解释: 最长的上升子序列是 [2,3,7,101],它的 ...

  5. [LeetCode] 300. 最长上升子序列 ☆☆☆(动态规划 二分)

    https://leetcode-cn.com/problems/longest-increasing-subsequence/solution/dong-tai-gui-hua-she-ji-fan ...

  6. LeetCode 300. 最长上升子序列(Longest Increasing Subsequence)

    题目描述 给出一个无序的整形数组,找到最长上升子序列的长度. 例如, 给出 [10, 9, 2, 5, 3, 7, 101, 18], 最长的上升子序列是 [2, 3, 7, 101],因此它的长度是 ...

  7. LeetCode 300——最长上升子序列

    1. 题目 2. 解答 2.1. 动态规划 我们定义状态 state[i] 表示以 nums[i] 为结尾元素的最长上升子序列的长度,那么状态转移方程为: \[state[i] = max(state ...

  8. 1. 线性DP 300. 最长上升子序列 (LIS)

    最经典单串: 300. 最长上升子序列 (LIS) https://leetcode-cn.com/problems/longest-increasing-subsequence/submission ...

  9. Leetcode题目300.最长上升子序列(动态规划-中等)

    题目描述: 给定一个无序的整数数组,找到其中最长上升子序列的长度. 示例: 输入: [10,9,2,5,3,7,101,18] 输出: 4 解释: 最长的上升子序列是 [2,3,7,101],它的长度 ...

  10. 【LeetCode】300.最长递增子序列——暴力递归(O(n^3)),动态规划(O(n^2)),动态规划+二分法(O(nlogn))

    算法新手,刷力扣遇到这题,搞了半天终于搞懂了,来这记录一下,欢迎大家交流指点. 题目描述: 给你一个整数数组 nums ,找到其中最长严格递增子序列的长度. 子序列是由数组派生而来的序列,删除(或不删 ...

随机推荐

  1. Codeforces 468C Hack it!

    https://www.luogu.org/problemnew/show/CF468C http://codeforces.com/contest/468/problem/C #include &l ...

  2. MQ如何解决消息的顺序性

    一.消息的顺序性 1.延迟队列:设置一个全局变量index,根据实际情况一次按照index++的逻辑一次给消息队列设置延迟时间段,可以是0.5s,甚至1s; 弊端:如果A,B,C..消息队列消费时间不 ...

  3. java8(1)--- lambda

    项目马上切java8了,之前对于java8的东西都是东打一棒西打一锤的了解了些.这次搜集整理了下,从lambda到stream相关的API等. 1.Lambda和匿名内部类 Lambda 是一个匿名的 ...

  4. 2019牛客多校训练第四场K.number(思维)

    题目传送门 题意: 输入一个只包含数字的字符串,求出是300的倍数的子串的个数(不同位置的0.00.000等都算,并考虑前导零的情况). sample input: 600 1230003210132 ...

  5. Python day01 课堂笔记

    今天是第一天学习Python课程,主要从计算机基础,Python的历史,环境 ,变量,常量,注释,用户交互,基础数据类型 ,简单的if条件语句和while循环语句这几个来学习,重点的掌握内容是pyth ...

  6. lombok 下的@Builder注解用法

    pom依赖 <dependency> <groupId>org.projectlombok</groupId>            <artifactId& ...

  7. Linux相关安装文档

    一.JDK环境安装 1.查看linux上是否存在已安装好的JDK (1)java -version openjdk version "1.8.0_181"OpenJDK Runti ...

  8. 使用maven搭建ssm框架环境

    1.前言 因为经常换环境,在搭ssm框架的时候老是出错,所以记录一下最近搭建的环境,以供参考. 本文讲解如何使用maven搭建ssm框架,并能用于简单的登录注册. IDE:IDEA,JDK版本:1.8 ...

  9. 以太坊智能合约[ERC20]发币记录

    以太坊被称为区块链2.0,就是因为以太坊在应用层提供了虚拟机,使得开发者可以基于它自定义逻辑,通常被称为智能合约,合约中的公共接口可以作为区块链中的普通交易执行.本文就智能合约发代币流程作一完整介绍( ...

  10. cookies和sessionstorage和localstorage区别

    相同点:客户端都会存储 不同点 不同点 存储大小 有效时间 数据与服务器交互方式 cookies <=4K 在设置cookie过期之前一直有效(无论窗口浏览器是否关闭) 正常情况下,cookie ...