当前位置:网站首页>Leetcode 300 longest ascending subsequence
Leetcode 300 longest ascending subsequence
2022-07-03 10:06:00 【Cute at the age of three @d】

Dynamic programming

class Solution {
public int lengthOfLIS(int[] nums) {
if (nums.length == 0) {
return 0;
}
int[] dp = new int[nums.length];
dp[0] = 1;
int maxans = 1;
for (int i = 1; i < nums.length; i++) {
dp[i] = 1;
for (int j = 0; j < i; j++) {
if (nums[i] > nums[j]) {
dp[i] = Math.max(dp[i], dp[j] + 1);
}
}
maxans = Math.max(maxans, dp[i]);
}
return maxans;
}
}
Dynamic programming + Two points

class Solution {
public int lengthOfLIS(int[] nums) {
//nums The length of
int n = nums.length;
// Dynamic programming array among dp[i] The storage length is i+1 The minimum value of the end number of the increasing subsequence of
int[] dp = new int[n];
int left = 0;
int right = 0;
Arrays.fill(dp,10001);
// Initialization length 1 String The minimum value at the end is nums[0]
dp[0] = nums[0];
for(int i = 1; i < n;i++){
int l = left;
int r = right;
// Look for less than nums[i] The maximum of
while(l < r){
int m = l + (r-l+1) / 2;
if(dp[m] >= nums[i])
r = m-1;
else
l = m;
}
if(dp[l] >= nums[i]){
// There is no less than nums[i] Number of numbers
dp[0] = Math.min(dp[0],nums[i]);
}
else{
dp[l+1] = Math.min(dp[l+1],nums[i]);
right = Math.max(right,l+1);
}
}
return right+1;
}
}
边栏推荐
- My notes on the development of intelligent charging pile (III): overview of the overall design of the system software
- Positive and negative sample division and architecture understanding in image classification and target detection
- 03 fastjason solves circular references
- Installation and removal of MySQL under Windows
- LeetCode - 895 最大频率栈(设计- 哈希表+优先队列 哈希表 + 栈) *
- LeetCode - 508. 出现次数最多的子树元素和 (二叉树的遍历)
- QT is a method of batch modifying the style of a certain type of control after naming the control
- Drive and control program of Dianchuan charging board for charging pile design
- QT setting suspension button
- openEuler kernel 技术分享 - 第1期 - kdump 基本原理、使用及案例介绍
猜你喜欢

LeetCode - 673. 最长递增子序列的个数

LeetCode 面试题 17.20. 连续中值(大顶堆+小顶堆)

openEuler kernel 技術分享 - 第1期 - kdump 基本原理、使用及案例介紹

When you need to use some functions of STM32, but 51 can't realize them, 32 naturally doesn't need to learn

LeetCode - 705 设计哈希集合(设计)

Opencv feature extraction - hog

An executable binary file contains more than machine instructions

Development of intelligent charging pile (I): overview of the overall design of the system

03 FastJson 解决循环引用

I think all friends should know that the basic law of learning is: from easy to difficult
随机推荐
Positive and negative sample division and architecture understanding in image classification and target detection
Opencv note 21 frequency domain filtering
Notes on C language learning of migrant workers majoring in electronic information engineering
YOLO_ V1 summary
Dictionary tree prefix tree trie
Vgg16 migration learning source code
Cases of OpenCV image enhancement
2021-11-11 standard thread library
QT detection card reader analog keyboard input
The 4G module designed by the charging pile obtains NTP time through mqtt based on 4G network
In third tier cities and counties, it is difficult to get 10K after graduation
使用密钥对的形式连接阿里云服务器
03 FastJson 解决循环引用
2020-08-23
openEuler kernel 技術分享 - 第1期 - kdump 基本原理、使用及案例介紹
Tensorflow built-in evaluation
Gif image analysis drawing RGB to YUV table lookup method to reduce CPU occupancy
Interruption system of 51 single chip microcomputer
STM32 general timer output PWM control steering gear
When you need to use some functions of STM32, but 51 can't realize them, 32 naturally doesn't need to learn