【LeetCode】673. Number of Longest Increasing Subsequence
题目:
Given an unsorted array of integers, find the number of longest increasing subsequence.
Example 1:
Input: [1,3,5,4,7]
Output: 2
Explanation: The two longest increasing subsequence are [1, 3, 4, 7] and [1, 3, 5, 7].
Example 2:
Input: [2,2,2,2,2]
Output: 5
Explanation: The length of longest continuous increasing subsequence is 1, and there are 5 subsequences' length is 1, so output 5.
题解:
首先解决最长的递增序列问题,最朴素的做法是深搜,以每一个数为开头,找位置在它后面的且数值比它大的为下一层,显然会超时,考虑用动态规划去解决问题(也就是最长上升序列(LIS),一个经典的动态规划问题)。其实就是LIS的变体。 摘自九章算法
1. 设dp[i]为以该数结尾,能构成的最长序列的长度。进行连接的时候,对于每个数字num[i],遍历位置在它之前的数字num[j],如果比这个数小(num[j]<num[i]),也就是能构成一个序列,这样就能进行状态转移,我们令dp[i]=max(dp[i],dp[j]+1)来保证存储的为最长长度,同时可以记录max(dp[i])
2. 考虑完题目的长度优先后,我们考虑数量,也就是说最长长度的序列有几个,这个问题需要我们在处理dp的时候来记录,我们设ans[i]为以第i个数结尾的最长序列的个数,与dp同理,ans初值也都是1
3. 状态转移的时候,如果dp更新了,也就是说(dp[j]+1>dp[i])说明这个长度的序列是新出现的,我们需要将ans[i]设置为ans[j],因为新序列中,最新的数提供了序列的尾巴,数量是由前面积累的(或者说转移);举例序列[1 1 3 7]我们易得数字3对应的dp=2,ans=2,因为可以构成两个[1 3]那么我们操作到数字7的时候,发现接在3后面最长,就可以转移ans来作为初始数量
4. 而当dp[j]+1==dp[i]的时候,如同样例,操作7的时候,我们最先发现了可以接在5后面,最长序列[1 3 5 7],然后发现可以接在4后面,[1 3 4 7],长度也是4,这时候就同样需要转移ans,加上去 ans[i]+=ans[j]
5. 最后我们需要遍历dp,找到dp[i]=我们记录的最大值的时候,累加我们得到的ans[i],即为所求结果,时间复杂度是O(n^2)
Solution
class Solution {
public:
int findNumberOfLIS(vector<int>& nums) {
int n = nums.size(), max_len = , res = ;
vector<int> dp(n, ), cnt(n, );
for(int i = ; i < n; ++i){
for(int j = ; j < i; ++j){
if(nums[j] < nums[i] && dp[j] + > dp[i]){
dp[i] = dp[j] + ;
cnt[i] = cnt[j];
} else if(nums[j] < nums[i] && dp[j] + == dp[i]){
cnt[i] += cnt[j];
}
}
max_len = max(max_len, dp[i]);
}
for(int i = ; i < n; ++i)
if(dp[i] == max_len) res += cnt[i];
return res;
}
};
【LeetCode】673. Number of Longest Increasing Subsequence的更多相关文章
- 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)
[LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...
- 【LeetCode】873. Length of Longest Fibonacci Subsequence 解题报告(Python)
[LeetCode]873. Length of Longest Fibonacci Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: ...
- Week 12 - 673.Number of Longest Increasing Subsequence
Week 12 - 673.Number of Longest Increasing Subsequence Given an unsorted array of integers, find the ...
- [LeetCode] 673. Number of Longest Increasing Subsequence 最长递增序列的个数
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
- LeetCode 673. Number of Longest Increasing Subsequence
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
- 673. Number of Longest Increasing Subsequence
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
- 673. Number of Longest Increasing Subsequence最长递增子序列的数量
[抄题]: Given an unsorted array of integers, find the number of longest increasing subsequence. Exampl ...
- [LeetCode] Number of Longest Increasing Subsequence 最长递增序列的个数
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
- LeetCode Number of Longest Increasing Subsequence
原题链接在这里:https://leetcode.com/problems/number-of-longest-increasing-subsequence/description/ 题目: Give ...
随机推荐
- python3 多线程编程
python / 并发 / 线程 / 对象 / 编程 0.什么是线程 1. 多线程模块 2. 创建线程的方法 3. join()方法 4.isAlive()方法 5. name属性和daemon属 ...
- 运维角度浅谈MySQL数据库优化
一个成熟的数据库架构并不是一开始设计就具备高可用.高伸缩等特性的,它是随着用户量的增加,基础架构才逐渐完善.这篇博文主要谈MySQL数据库发展周期中所面临的问题及优化方案,暂且抛开前端应用不说,大致分 ...
- 面向服务体系架构(SOA)和数据仓库(DW)的思考
摘要: 当前业界对面向服务体系架构(SOA)和数据仓库(Data Warehouse,DW)都介绍的很多,提出了很多优秀的解决方案,但是一般是把 SOA 和 DW 单独考虑,SOA 和 DW 有着共同 ...
- 吴恩达深度学习笔记(九) —— FaceNet
主要内容: 一.FaceNet人脸识别简介 二.使用神经网络对人脸进行编码 三.代价函数triple loss 四.人脸库 五.人脸认证与人脸识别 一.FaceNet简介 1.FaceNet是一个深层 ...
- POJ 3159 最短路 SPFA
#include<iostream> using namespace std; const int nMax = 30005; const int mMax = 150005; const ...
- ssh框架整合意义
一次次学习,一次次不一样的进一步理解. 一.Struts2.String.Hibernate框架的整合的意义: 1.需要将所有的对象进行统一管理(action动作类:sessionFactory) 2 ...
- C# 处理base64 以及base64的原理分析
base64的原理, http://www.cnblogs.com/diligenceday/p/6002382.html http://www.cnblogs.com/chengxiaohui/ar ...
- sql server parameter validation of stored procedure
https://stackoverflow.com/questions/41908156/validating-missing-parameter-from-procedure-calls I don ...
- HIVE HSQL 基本操作命令
创建表: hive>create table tablename(id int,name string,password string); 创建一个名字为tablename的表,表的属性有int ...
- 字符在内存中最终的表示形式是什么?是某种字符编码还是码位(Code Point)?
字符在内存中最终的表示形式是什么?是某种字符编码还是码位(Code Point)? 根据我的了解,编码中有三个核心概念:1. 字符集(Character Set),可以说是一个抽象概念,字符的合集2. ...