当前位置:网站首页>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;
}
}
边栏推荐
- 在三线城市、在县城,很难毕业就拿到10K
- 2. Elment UI date selector formatting problem
- Drive and control program of Dianchuan charging board for charging pile design
- Introduction to chromium embedded framework (CEF)
- Fundamentals of Electronic Technology (III)__ Fundamentals of circuit analysis__ Basic amplifier operating principle
- 4G module IMEI of charging pile design
- 【力扣刷题笔记(二)】特别技巧,模块突破,45道经典题目分类总结,在不断巩固中精进
- 4G module designed by charging pile obtains signal strength and quality
- MySQL的简单使用(增删改查)
- [Li Kou brush question notes (II)] special skills, module breakthroughs, classification and summary of 45 classic questions, and refinement in continuous consolidation
猜你喜欢
Eight working modes of stm32gpio and chip naming rules
Comment la base de données mémoire joue - t - elle l'avantage de la mémoire?
It is difficult to quantify the extent to which a single-chip computer can find a job
应用最广泛的8位单片机当然也是初学者们最容易上手学习的单片机
Fundamentals of Electronic Technology (III)__ Logic gate symbols in Chapter 5
Getting started with JMX, MBean, mxbean, mbeanserver
Uniapp realizes global sharing of wechat applet and custom sharing button style
Basic knowledge of communication interface
Assignment to '*' form incompatible pointer type 'linkstack' {aka '*'} problem solving
There is no shortcut to learning and development, and there is almost no situation that you can learn faster by leading the way
随机推荐
2.Elment Ui 日期选择器 格式化问题
Stm32f407 key interrupt
uniapp 实现微信小程序全局分享及自定义分享按钮样式
You need to use MySQL in the opening experiment. How can you forget the basic select statement? Remedy is coming~
El table X-axis direction (horizontal) scroll bar slides to the right by default
Fundamentals of Electronic Technology (III)_ Chapter 2 principle of amplification circuit__ Crystal triode and field effect triode
Application of external interrupts
Introduction to chromium embedded framework (CEF)
Screen display of charging pile design -- led driver ta6932
Project cost management__ Plan value_ Earned value_ Relationship among actual cost and Countermeasures
03 fastjason solves circular references
Hal library sets STM32 clock
2. Elment UI date selector formatting problem
getopt_ Typical use of long function
The third paper of information system project manager in soft examination
Stm32 NVIC interrupt priority management
Serial communication based on 51 single chip microcomputer
The new series of MCU also continues the two advantages of STM32 product family: low voltage and energy saving
手机都算是单片机的一种,只不过它用的硬件不是51的芯片
STM32 external interrupt experiment