当前位置:网站首页>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;
}
}
边栏推荐
- 要選擇那種語言為單片機編寫程序呢
- Google browser plug-in recommendation
- Stm32f04 clock configuration
- After clicking the Save button, you can only click it once
- 没有多少人能够最终把自己的兴趣带到大学毕业上
- Qcombox style settings
- [combinatorics] Introduction to Combinatorics (combinatorial idea 3: upper and lower bound approximation | upper and lower bound approximation example Remsey number)
- 03 fastjason solves circular references
- It is difficult to quantify the extent to which a single-chip computer can find a job
- Blue Bridge Cup for migrant workers majoring in electronic information engineering
猜你喜欢
随机推荐
Opencv notes 20 PCA
Liquid crystal display
Opencv gray histogram, histogram specification
嵌入式本来就很坑,相对于互联网来说那个坑多得简直是难走
Window maximum and minimum settings
Which language should I choose to program for single chip microcomputer
JS基础-原型原型链和宏任务/微任务/事件机制
2. Elment UI date selector formatting problem
Stm32f04 clock configuration
03 FastJson 解决循环引用
Getting started with JMX, MBean, mxbean, mbeanserver
2020-08-23
Development of intelligent charging pile (I): overview of the overall design of the system
SCM is now overwhelming, a wide variety, so that developers are overwhelmed
LeetCode - 895 最大频率栈(设计- 哈希表+优先队列 哈希表 + 栈) *
Education is a pass and ticket. With it, you can step into a higher-level environment
(1) What is a lambda expression
(2)接口中新增的方法
4G module at command communication package interface designed by charging pile
Notes on C language learning of migrant workers majoring in electronic information engineering









