当前位置:网站首页>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;
}
}
边栏推荐
- Of course, the most widely used 8-bit single chip microcomputer is also the single chip microcomputer that beginners are most easy to learn
- Leetcode 300 最长上升子序列
- Opencv gray histogram, histogram specification
- Getting started with JMX, MBean, mxbean, mbeanserver
- The underlying principle of vector
- Yocto technology sharing phase IV: customize and add software package support
- LeetCode - 1670 设计前中后队列(设计 - 两个双端队列)
- [untitled] proteus simulation of traffic lights based on 89C51 Single Chip Microcomputer
- SCM is now overwhelming, a wide variety, so that developers are overwhelmed
- 4G module board level control interface designed by charging pile
猜你喜欢
Installation and removal of MySQL under Windows
yocto 技术分享第四期:自定义增加软件包支持
el-table X轴方向(横向)滚动条默认滑到右边
要選擇那種語言為單片機編寫程序呢
Not many people can finally bring their interests to college graduation
[combinatorics] Introduction to Combinatorics (combinatorial idea 3: upper and lower bound approximation | upper and lower bound approximation example Remsey number)
QT is a method of batch modifying the style of a certain type of control after naming the control
yocto 技術分享第四期:自定義增加軟件包支持
Swing transformer details-2
03 FastJson 解决循环引用
随机推荐
LeetCode - 703 数据流中的第 K 大元素(设计 - 优先队列)
It is difficult to quantify the extent to which a single-chip computer can find a job
getopt_ Typical use of long function
QT setting suspension button
应用最广泛的8位单片机当然也是初学者们最容易上手学习的单片机
Programming ideas are more important than anything, not more than who can use several functions, but more than the understanding of the program
is_ power_ of_ 2 judge whether it is a multiple of 2
Tensorflow built-in evaluation
4G module initialization of charge point design
QT self drawing button with bubbles
51 MCU tmod and timer configuration
There is no shortcut to learning and development, and there is almost no situation that you can learn faster by leading the way
2020-08-23
Education is a pass and ticket. With it, you can step into a higher-level environment
Opencv note 21 frequency domain filtering
LeetCode - 1670 设计前中后队列(设计 - 两个双端队列)
01仿B站项目业务架构
ADS simulation design of class AB RF power amplifier
I think all friends should know that the basic law of learning is: from easy to difficult
01仿B站项目业务架构