当前位置:网站首页>Leetcode 300 最长上升子序列
Leetcode 300 最长上升子序列
2022-07-03 09:20:00 【三岁就很萌@D】
动态规划
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;
}
}
动态规划+二分
class Solution {
public int lengthOfLIS(int[] nums) {
//nums的长度
int n = nums.length;
//动态规划数组 其中dp[i] 存放长度为i+1的递增子序列的末尾数的最小值
int[] dp = new int[n];
int left = 0;
int right = 0;
Arrays.fill(dp,10001);
//初始化长度1的字符串 结尾最小值是nums[0]
dp[0] = nums[0];
for(int i = 1; i < n;i++){
int l = left;
int r = right;
// 寻找小于nums[i] 的最大值
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]){
//不存在小于nums[i]的数
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;
}
}
边栏推荐
- Which language should I choose to program for single chip microcomputer
- 4G module board level control interface designed by charging pile
- [untitled] proteus simulation of traffic lights based on 89C51 Single Chip Microcomputer
- STM32 external interrupt experiment
- Getting started with JMX, MBean, mxbean, mbeanserver
- Fundamentals of Electronic Technology (III)_ Integrated operational amplifier and its application__ Basic arithmetic circuit
- STM32 interrupt switch
- 嵌入式系统没有特别明确的定义
- Introduction to chromium embedded framework (CEF)
- JS基础-原型原型链和宏任务/微任务/事件机制
猜你喜欢
03 FastJson 解决循环引用
Eight working modes of stm32gpio and chip naming rules
2.Elment Ui 日期选择器 格式化问题
JS foundation - prototype prototype chain and macro task / micro task / event mechanism
Open Euler Kernel Technology Sharing - Issue 1 - kdump Basic Principles, use and Case Introduction
uniapp 实现微信小程序全局分享及自定义分享按钮样式
It is difficult to quantify the extent to which a single-chip computer can find a job
Development of intelligent charging pile (I): overview of the overall design of the system
03 fastjason solves circular references
在三线城市、在县城,很难毕业就拿到10K
随机推荐
NR PUCCH format0 sequence generation and detection mechanism
Gpiof6, 7, 8 configuration
Raspberry pie installation SciPy
You need to use MySQL in the opening experiment. How can you forget the basic select statement? Remedy is coming~
Characteristics of PUCCH formats
[untitled] proteus simulation of traffic lights based on 89C51 Single Chip Microcomputer
Exception handling of arm
4G module designed by charging pile obtains signal strength and quality
In third tier cities and counties, it is difficult to get 10K after graduation
2021-10-27
对于新入行的同学,如果你完全没有接触单片机,建议51单片机入门
STM32 external interrupt experiment
手机都算是单片机的一种,只不过它用的硬件不是51的芯片
Installation and removal of MySQL under Windows
Stm32f04 clock configuration
Hal library sets STM32 clock
01仿B站项目业务架构
The third paper of information system project manager in soft examination
STM32 general timer 1s delay to realize LED flashing
STM32 serial port usart1 routine