当前位置:网站首页>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;
}
}
边栏推荐
- Opencv feature extraction - hog
- Vgg16 migration learning source code
- I didn't think so much when I was in the field of single chip microcomputer. I just wanted to earn money to support myself first
- The data read by pandas is saved to the MySQL database
- RESNET code details
- Oracle database SQL statement execution plan, statement tracking and optimization instance
- Exception handling of arm
- Of course, the most widely used 8-bit single chip microcomputer is also the single chip microcomputer that beginners are most easy to learn
- About windows and layout
- 4G module designed by charging pile obtains signal strength and quality
猜你喜欢
openEuler kernel 技术分享 - 第1期 - kdump 基本原理、使用及案例介绍
Basic knowledge of communication interface
03 fastjason solves circular references
当你需要使用STM32某些功能,而51实现不了时, 那32自然不需要学
Stm32f407 key interrupt
LeetCode - 673. 最长递增子序列的个数
Openeuler kernel technology sharing - Issue 1 - kdump basic principle, use and case introduction
Interruption system of 51 single chip microcomputer
Cases of OpenCV image enhancement
新系列单片机还延续了STM32产品家族的低电压和节能两大优势
随机推荐
Crash工具基本使用及实战分享
There is no shortcut to learning and development, and there is almost no situation that you can learn faster by leading the way
LeetCode - 508. 出现次数最多的子树元素和 (二叉树的遍历)
Which language should I choose to program for single chip microcomputer
嵌入式本来就很坑,相对于互联网来说那个坑多得简直是难走
Opencv interview guide
Simulate mouse click
Toolbutton property settings
JS foundation - prototype prototype chain and macro task / micro task / event mechanism
使用密钥对的形式连接阿里云服务器
2312、卖木头块 | 面试官与狂徒张三的那些事(leetcode,附思维导图 + 全部解法)
2. Elment UI date selector formatting problem
RESNET code details
SCM career development: those who can continue to do it have become great people. If they can't endure it, they will resign or change their careers
2021-11-11 standard thread library
Tensorflow built-in evaluation
[combinatorics] combinatorial existence theorem (three combinatorial existence theorems | finite poset decomposition theorem | Ramsey theorem | existence theorem of different representative systems |
The 4G module designed by the charging pile obtains NTP time through mqtt based on 4G network
03 fastjason solves circular references
Not many people can finally bring their interests to college graduation