当前位置:网站首页>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;
}
}
边栏推荐
- LeetCode - 703 数据流中的第 K 大元素(设计 - 优先队列)
- Working mode of 80C51 Serial Port
- Adaptiveavgpool1d internal implementation
- My notes on the development of intelligent charging pile (III): overview of the overall design of the system software
- LeetCode - 673. 最长递增子序列的个数
- 我想各位朋友都应该知道学习的基本规律就是:从易到难
- 51 MCU tmod and timer configuration
- Crash工具基本使用及实战分享
- openEuler kernel 技术分享 - 第1期 - kdump 基本原理、使用及案例介绍
- (1) What is a lambda expression
猜你喜欢

03 FastJson 解决循环引用

Stm32f407 key interrupt

Opencv notes 17 template matching

Blue Bridge Cup for migrant workers majoring in electronic information engineering

LeetCode - 706 设计哈希映射(设计) *

YOLO_ V1 summary

el-table X轴方向(横向)滚动条默认滑到右边

Embedded systems are inherently flawed. Compared with the Internet, there are so many holes that it is simply difficult to walk away from

Opencv note 21 frequency domain filtering

Opencv feature extraction - hog
随机推荐
RESNET code details
自动装箱与拆箱了解吗?原理是什么?
我想各位朋友都应该知道学习的基本规律就是:从易到难
LeetCode - 706 设计哈希映射(设计) *
The underlying principle of vector
Emballage automatique et déballage compris? Quel est le principe?
Leetcode bit operation
Serial port programming
There is no specific definition of embedded system
LeetCode - 508. 出现次数最多的子树元素和 (二叉树的遍历)
(1) 什么是Lambda表达式
Pymssql controls SQL for Chinese queries
Toolbutton property settings
Problems encountered when MySQL saves CSV files
新系列单片机还延续了STM32产品家族的低电压和节能两大优势
JS foundation - prototype prototype chain and macro task / micro task / event mechanism
Cases of OpenCV image enhancement
2021-11-11 standard thread library
JS基础-原型原型链和宏任务/微任务/事件机制
Working mode of 80C51 Serial Port