当前位置:网站首页>LeetCode - 673. 最长递增子序列的个数
LeetCode - 673. 最长递增子序列的个数
2022-07-03 09:20:00 【三岁就很萌@D】

动态规划

class Solution {
public int findNumberOfLIS(int[] nums) {
int n = nums.length;
//0 维表示以nums[i]结尾的最长子序列长度 1 维 表示以nums[i]结尾的最长子序列个数
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;
}
}
贪心 + 前缀和 + 二分查找

class Solution {
public int findNumberOfLIS(int[] nums) {
int n = nums.length;
//val.get(i) 存放了长度为i+1的递增子序列 的末尾元素 val.get(i) 单调不增
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;
}
}
边栏推荐
- [untitled] proteus simulation of traffic lights based on 89C51 Single Chip Microcomputer
- Gif image analysis drawing RGB to YUV table lookup method to reduce CPU occupancy
- Happy Dragon Boat Festival—— Zongzi written by canvas~~~~~
- NR PUCCH format0 sequence generation and detection mechanism
- Notes on C language learning of migrant workers majoring in electronic information engineering
- 自动装箱与拆箱了解吗?原理是什么?
- El table X-axis direction (horizontal) scroll bar slides to the right by default
- CEF下载,编译工程
- Fundamentals of Electronic Technology (III)__ Chapter 6 combinational logic circuit
- SCM career development: those who can continue to do it have become great people. If they can't endure it, they will resign or change their careers
猜你喜欢

El table X-axis direction (horizontal) scroll bar slides to the right by default

Happy Dragon Boat Festival—— Zongzi written by canvas~~~~~

编程思想比任何都重要,不是比谁多会用几个函数而是比程序的理解

03 fastjason solves circular references

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

SCM career development: those who can continue to do it have become great people. If they can't endure it, they will resign or change their careers

Oracle database SQL statement execution plan, statement tracking and optimization instance

在三线城市、在县城,很难毕业就拿到10K

STM32 interrupt priority management

SCM is now overwhelming, a wide variety, so that developers are overwhelmed
随机推荐
Introduction to chromium embedded framework (CEF)
Project scope management__ Scope management plan and scope specification
GPIO port details, Hal library operation keys
自动装箱与拆箱了解吗?原理是什么?
(1) 什么是Lambda表达式
内存数据库究竟是如何发挥内存优势的?
Notes on C language learning of migrant workers majoring in electronic information engineering
Windows下MySQL的安装和删除
QT qcombobox QSS style settings
2020-08-23
单片机现在可谓是铺天盖地,种类繁多,让开发者们应接不暇
Basic knowledge of MySQL database (an introduction to systematization)
JS foundation - prototype prototype chain and macro task / micro task / event mechanism
2021-01-03
Crash工具基本使用及实战分享
Uniapp realizes global sharing of wechat applet and custom sharing button style
openEuler kernel 技术分享 - 第1期 - kdump 基本原理、使用及案例介绍
Runtime. getRuntime(). GC () and runtime getRuntime(). The difference between runfinalization()
Gif image analysis drawing RGB to YUV table lookup method to reduce CPU occupancy
Serial port programming