【LeetCode】300-最长上升子序列
题目描述
给定一个无序的整数数组,找到其中最长上升子序列的长度。
示例:
输入: [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, ortoIndexif 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- iffromIndex > toIndex
ArrayIndexOutOfBoundsException- iffromIndex < 0 or toIndex > a.length
- Since:
1.6
【LeetCode】300-最长上升子序列的更多相关文章
- Java实现 LeetCode 300 最长上升子序列
300. 最长上升子序列 给定一个无序的整数数组,找到其中最长上升子序列的长度. 示例: 输入: [10,9,2,5,3,7,101,18] 输出: 4 解释: 最长的上升子序列是 [2,3,7,10 ...
- leetcode 300最长上升子序列
用递归DFS遍历所有组合肯定积分会超时,原因是有很多重复的操作,可以想象每次回溯后肯定会有重复操作.所以改用动态规划.建立一个vector<int>memo,初始化为1,memo[i]表示 ...
- Leetcode——300. 最长上升子序列
题目描述:题目链接 给定一个无序的整数数组,找到其中最长上升子序列的长度. 示例: 输入: [10,9,2,5,3,7,101,18] 输出: 4 解释: 最长的上升子序列是 [2,3,7,101], ...
- Leetcode 300.最长上升子序列
最长上升子序列 给定一个无序的整数数组,找到其中最长上升子序列的长度. 示例: 输入: [10,9,2,5,3,7,101,18] 输出: 4 解释: 最长的上升子序列是 [2,3,7,101],它的 ...
- [LeetCode] 300. 最长上升子序列 ☆☆☆(动态规划 二分)
https://leetcode-cn.com/problems/longest-increasing-subsequence/solution/dong-tai-gui-hua-she-ji-fan ...
- LeetCode 300. 最长上升子序列(Longest Increasing Subsequence)
题目描述 给出一个无序的整形数组,找到最长上升子序列的长度. 例如, 给出 [10, 9, 2, 5, 3, 7, 101, 18], 最长的上升子序列是 [2, 3, 7, 101],因此它的长度是 ...
- LeetCode 300——最长上升子序列
1. 题目 2. 解答 2.1. 动态规划 我们定义状态 state[i] 表示以 nums[i] 为结尾元素的最长上升子序列的长度,那么状态转移方程为: \[state[i] = max(state ...
- 1. 线性DP 300. 最长上升子序列 (LIS)
最经典单串: 300. 最长上升子序列 (LIS) https://leetcode-cn.com/problems/longest-increasing-subsequence/submission ...
- Leetcode题目300.最长上升子序列(动态规划-中等)
题目描述: 给定一个无序的整数数组,找到其中最长上升子序列的长度. 示例: 输入: [10,9,2,5,3,7,101,18] 输出: 4 解释: 最长的上升子序列是 [2,3,7,101],它的长度 ...
- 【LeetCode】300.最长递增子序列——暴力递归(O(n^3)),动态规划(O(n^2)),动态规划+二分法(O(nlogn))
算法新手,刷力扣遇到这题,搞了半天终于搞懂了,来这记录一下,欢迎大家交流指点. 题目描述: 给你一个整数数组 nums ,找到其中最长严格递增子序列的长度. 子序列是由数组派生而来的序列,删除(或不删 ...
随机推荐
- React-Native之打包发布(Android)
React-Native之打包发布(Android) 一,介绍与需求 移动端打包发布到应用市场 二,发布配置 注意:以下所有操作都在win10下进行,React Native版本0.59.5,andr ...
- Codeforces Round #574 (Div. 2)——C. Basketball Exercise(简单DP)
题目传送门 题意: 输入n,给出两组均为 n个数字的数组a和b,轮流从a和b数组中取出一个数字,要求严格按照当前所选数字的数组下标比上一个所选数字的数组下标更大,计算能够取出的数字加起来的总和最大能为 ...
- requestAnimationFrame 兼容方案
[toc] 编写涉及:css, html, js 在线演示codepen html代码 <div class="roll-box"> <div class=&qu ...
- android ——活动
活动(Activity)主要用于和用户进行交互,是一种可以包含用户界面的组件. 1.手动创建活动 右击com.example.administrator.exp5→New→Activity→Empty ...
- MacOS VSCode 安装 GO 插件失败问题解决
0x00 问题重现 Installing golang.org/x/tools/cmd/guru FAILED Installing golang.org/x/tools/cmd/gorename F ...
- 为什么要用Kubernetes?
1.前言 第一次接触Kubernetes是在2016年,再一次浏览博文的时候,那是我第一次听到Kubernetes这个名词,也是第一次认识了k8s这么一个东西.后来在慢慢了解它的时候,被它天生高可用. ...
- RSA加密的java实现
首先科普一波: RSA的1024位是指公钥及私钥分别是1024bit,也就是1024/8=128 Bytes RSA算法密钥长度的选择是安全性和程序性能平衡的结果,密钥长度越长,安全性越好,加密解密所 ...
- 操作微信-itchat库的安装
基于pyCharm开发环境,在CMD控制台输入:pip install itchat 等待安装...... Microsoft Windows [版本 6.1.7601]版权所有 (c) 2 ...
- SSH密码和秘钥认证原理
SSH登录方式主要分为两种: 1. 用户名密码验证方式 说明: (1) 当客户端发起ssh请求,服务器会把自己的公钥发送给用户: (2) 用户会根据服务器发来的公钥对密码进行加密: (3) 加密后的信 ...
- python学习——字典和集合
一.字典 1)字典介绍 字典是一种通过名字或者关键字引用的得数据结构,其键可以是数字.字符串.元组,这种不可变的结构类型也称之为映射.字典类型是Python中唯一內建的映射类型. 1)字典操作 &qu ...