当前位置:网站首页>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;
}
}
边栏推荐
- Wireshark use
- openEuler kernel 技术分享 - 第1期 - kdump 基本原理、使用及案例介绍
- Exception handling of arm
- The underlying principle of vector
- 学习开发没有捷径,也几乎不存在带路会学的快一些的情况
- 自動裝箱與拆箱了解嗎?原理是什麼?
- Serial communication based on 51 single chip microcomputer
- There is no specific definition of embedded system
- 01仿B站项目业务架构
- 应用最广泛的8位单片机当然也是初学者们最容易上手学习的单片机
猜你喜欢

03 FastJson 解决循环引用

Development of intelligent charging pile (I): overview of the overall design of the system

The underlying principle of vector

没有多少人能够最终把自己的兴趣带到大学毕业上

03 fastjason solves circular references

Yocto Technology Sharing Phase 4: Custom add package support

应用最广泛的8位单片机当然也是初学者们最容易上手学习的单片机

SCM is now overwhelming, a wide variety, so that developers are overwhelmed

Leetcode 300 最长上升子序列

Retinaface: single stage dense face localization in the wild
随机推荐
Opencv Harris corner detection
The underlying principle of vector
2021-01-03
yocto 技術分享第四期:自定義增加軟件包支持
Basic knowledge of MySQL database (an introduction to systematization)
My notes on the development of intelligent charging pile (III): overview of the overall design of the system software
使用sed替换文件夹下文件
el-table X轴方向(横向)滚动条默认滑到右边
openEuler kernel 技術分享 - 第1期 - kdump 基本原理、使用及案例介紹
Crash工具基本使用及实战分享
pycharm 无法引入自定义包
要選擇那種語言為單片機編寫程序呢
My 4G smart charging pile gateway design and development related articles
Leetcode 300 最长上升子序列
Swing transformer details-1
About windows and layout
Installation and removal of MySQL under Windows
After clicking the Save button, you can only click it once
Oracle database SQL statement execution plan, statement tracking and optimization instance
Seven sorting of ten thousand words by hand (code + dynamic diagram demonstration)