当前位置:网站首页>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;
}
}
边栏推荐
- Which language should I choose to program for single chip microcomputer
- 2.Elment Ui 日期选择器 格式化问题
- 使用sed替换文件夹下文件
- 4G module at command communication package interface designed by charging pile
- A lottery like scissors, stone and cloth (C language)
- Adaptiveavgpool1d internal implementation
- LeetCode - 933 最近的请求次数
- Mobile phones are a kind of MCU, but the hardware it uses is not 51 chip
- LeetCode - 673. 最长递增子序列的个数
- Dictionary tree prefix tree trie
猜你喜欢

Blue Bridge Cup for migrant workers majoring in electronic information engineering

LeetCode - 508. 出现次数最多的子树元素和 (二叉树的遍历)

Opencv histogram equalization

QT is a method of batch modifying the style of a certain type of control after naming the control

Installation and removal of MySQL under Windows

学习开发没有捷径,也几乎不存在带路会学的快一些的情况

LeetCode - 919. 完全二叉树插入器 (数组)

I think all friends should know that the basic law of learning is: from easy to difficult

When you need to use some functions of STM32, but 51 can't realize them, 32 naturally doesn't need to learn

Uniapp realizes global sharing of wechat applet and custom sharing button style
随机推荐
LeetCode - 919. 完全二叉树插入器 (数组)
Yocto technology sharing phase IV: customize and add software package support
Crash工具基本使用及实战分享
el-table X轴方向(横向)滚动条默认滑到右边
Timer and counter of 51 single chip microcomputer
yocto 技術分享第四期:自定義增加軟件包支持
Installation and removal of MySQL under Windows
Swing transformer details-1
Opencv gray histogram, histogram specification
LeetCode - 1670 设计前中后队列(设计 - 两个双端队列)
RESNET code details
2020-08-23
(2) New methods in the interface
Vgg16 migration learning source code
Opencv note 21 frequency domain filtering
Gif image analysis drawing RGB to YUV table lookup method to reduce CPU occupancy
Basic knowledge of communication interface
MySQL root user needs sudo login
2312、卖木头块 | 面试官与狂徒张三的那些事(leetcode,附思维导图 + 全部解法)
01仿B站项目业务架构