当前位置:网站首页>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;
}
}
边栏推荐
- Dynamic layout management
- There is no specific definition of embedded system
- Crash工具基本使用及实战分享
- Simple use of MySQL (addition, deletion, modification and query)
- Toolbutton property settings
- Not many people can finally bring their interests to college graduation
- 单片机职业发展:能做下去的都成牛人了,熬不动就辞职或者改行了
- Dictionary tree prefix tree trie
- 2021-11-11 standard thread library
- (2) New methods in the interface
猜你喜欢
2312、卖木头块 | 面试官与狂徒张三的那些事(leetcode,附思维导图 + 全部解法)
Uniapp realizes global sharing of wechat applet and custom sharing button style
Vgg16 migration learning source code
嵌入式本来就很坑,相对于互联网来说那个坑多得简直是难走
Pymssql controls SQL for Chinese queries
51 MCU tmod and timer configuration
Opencv histogram equalization
03 FastJson 解决循环引用
LeetCode - 919. 完全二叉树插入器 (数组)
Opencv notes 20 PCA
随机推荐
Application of 51 single chip microcomputer timer
LeetCode - 933 最近的请求次数
嵌入式本来就很坑,相对于互联网来说那个坑多得简直是难走
Gif image analysis drawing RGB to YUV table lookup method to reduce CPU occupancy
The data read by pandas is saved to the MySQL database
Opencv notes 17 template matching
QT setting suspension button
4G module IMEI of charging pile design
Design of charging pile mqtt transplantation based on 4G EC20 module
STM32 running lantern experiment - library function version
el-table X轴方向(横向)滚动条默认滑到右边
4G module designed by charging pile obtains signal strength and quality
is_ power_ of_ 2 judge whether it is a multiple of 2
Mobile phones are a kind of MCU, but the hardware it uses is not 51 chip
ADS simulation design of class AB RF power amplifier
没有多少人能够最终把自己的兴趣带到大学毕业上
Open Euler Kernel Technology Sharing - Issue 1 - kdump Basic Principles, use and Case Introduction
LeetCode - 508. 出现次数最多的子树元素和 (二叉树的遍历)
STM32 general timer 1s delay to realize LED flashing
Windows下MySQL的安装和删除