当前位置:网站首页>LeetCode - 673. Number of longest increasing subsequences
LeetCode - 673. Number of longest increasing subsequences
2022-07-03 10:06:00 【Cute at the age of three @d】

Dynamic programming

class Solution {
public int findNumberOfLIS(int[] nums) {
int n = nums.length;
//0 Dimension is represented by nums[i] The length of the longest subsequence at the end 1 dimension Said to nums[i] Number of longest subsequences at the end
int[][] dp = new int[n][2];
dp[0][0] = 1;
dp[0][1] = 1;
int maxlen = 1;
int maxnum = 1;
for(int i = 1; i < n;i++){
dp[i][0] = 1;
dp[i][1] = 1;
for(int j = 0; j < i;j++){
if(nums[j] < nums[i]){
//dp[i] = Math.max(dp[i],dp[j] +1);
if(dp[j][0]+1 == dp[i][0]){
dp[i][1] += dp[j][1];
}
if(dp[j][0]+1 > dp[i][0]){
dp[i][0] = dp[j][0] + 1;
dp[i][1] = dp[j][1];
}
}
}
if(dp[i][0] == maxlen)
maxnum += dp[i][1];
if(dp[i][0] > maxlen){
maxlen = dp[i][0];
maxnum = dp[i][1];
}
}
return maxnum;
}
}
greedy + The prefix and + Two points search

class Solution {
public int findNumberOfLIS(int[] nums) {
int n = nums.length;
//val.get(i) Stored with a length of i+1 The increasing subsequence of End element of val.get(i) Monotony does not increase
List<List<Integer>> val = new ArrayList<>();
List<List<Integer>> presum = new ArrayList<>();
List<Integer> val0 = new ArrayList<>();
val0.add(nums[0]);
List<Integer> presum0 = new ArrayList<>();
presum0.add(1);
val.add(val0);
presum.add(presum0);
int left = 0;
int right = 0;
int maxlen = 1;
int maxsum = 1;
for(int i = 1; i < n;i++){
int l = left;
int r = right;
while(l < r){
int m = l + (r - l +1) / 2;
int len = val.get(m).size();
if(val.get(m).get(len-1) < nums[i])
l = m;
else
r = m-1;
}
if(val.get(l).get(val.get(l).size()-1) >= nums[i]){
val.get(l).add(nums[i]);
presum.get(l).add(presum.get(l).get(presum.get(l).size()-1)+1);
if(l+1 == maxlen)
maxsum += 1;
}
else{
System.out.println("****");
List<Integer> smallVal = val.get(l);
List<Integer> smallPre = presum.get(l);
int l1 = 0;
int r1 = smallVal.size() - 1;
while(l1 < r1){
int m1 = l1 + (r1-l1) / 2;
if(smallVal.get(m1) >= nums[i]){
l1 = m1+1;
}
else{
r1 = m1;
}
}
int pre = 0;
if(l1 == 0)
pre = smallPre.get(smallPre.size()-1);
else
pre = smallPre.get(smallPre.size()-1) - smallPre.get(l1-1) ;
System.out.println(pre);
if(right < l+1){
List<Integer> newval = new ArrayList<>();
List<Integer> newpre = new ArrayList<>();
newval.add(nums[i]);
newpre.add(pre);
val.add(newval);
presum.add(newpre);
right = l+1;
}else{
int lastpre = presum.get(l+1).get( presum.get(l+1).size()-1);
val.get(l+1).add(nums[i]);
presum.get(l+1).add(pre+lastpre);
}
if(l+2 == maxlen)
maxsum += pre;
if(l+2 > maxlen){
maxlen = l+2;
maxsum = pre;
}
}
}
return maxsum;
}
}
边栏推荐
- LeetCode - 919. 完全二叉树插入器 (数组)
- STM32 running lantern experiment - library function version
- 2021-11-11 standard thread library
- About windows and layout
- Positive and negative sample division and architecture understanding in image classification and target detection
- Timer and counter of 51 single chip microcomputer
- Stm32f407 key interrupt
- getopt_ Typical use of long function
- Sending and interrupt receiving of STM32 serial port
- 2020-08-23
猜你喜欢

The underlying principle of vector

Serial communication based on 51 single chip microcomputer

Mobile phones are a kind of MCU, but the hardware it uses is not 51 chip

JS基础-原型原型链和宏任务/微任务/事件机制

Stm32f407 key interrupt

Blue Bridge Cup for migrant workers majoring in electronic information engineering

03 fastjason solves circular references

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

2312、卖木头块 | 面试官与狂徒张三的那些事(leetcode,附思维导图 + 全部解法)

Opencv histogram equalization
随机推荐
[keil5 debugging] warning:enumerated type mixed with other type
2020-08-23
openEuler kernel 技术分享 - 第1期 - kdump 基本原理、使用及案例介绍
An executable binary file contains more than machine instructions
2.Elment Ui 日期选择器 格式化问题
Programming ideas are more important than anything, not more than who can use several functions, but more than the understanding of the program
自动装箱与拆箱了解吗?原理是什么?
QT self drawing button with bubbles
03 FastJson 解决循环引用
openEuler kernel 技術分享 - 第1期 - kdump 基本原理、使用及案例介紹
LeetCode - 673. 最长递增子序列的个数
Qcombox style settings
Google browser plug-in recommendation
Opencv notes 17 template matching
Dictionary tree prefix tree trie
Seven sorting of ten thousand words by hand (code + dynamic diagram demonstration)
LeetCode - 460 LFU 缓存(设计 - 哈希表+双向链表 哈希表+平衡二叉树(TreeSet))*
Tensorflow built-in evaluation
对于新入行的同学,如果你完全没有接触单片机,建议51单片机入门
LeetCode - 919. 完全二叉树插入器 (数组)