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.
Note: Length of the given array will be not exceed 2000 and the answer is guaranteed to be fit in 32-bit signed int.
Approach #1: C++. [DFS]
class Solution {
public:
int findNumberOfLIS(vector<int>& nums) {
int n = nums.size();
if (n == 0) return 0; c_ = vector<int>(n, 0);
l_ = vector<int>(n, 0); int max_len = 0;
for (int i = 0; i < n; ++i)
max_len = max(max_len, len(nums, i)); int ans = 0;
for (int i = 0; i < n; ++i)
if (len(nums, i) == max_len)
ans += count(nums, i); return ans;
} private:
vector<int> c_;
vector<int> l_; // find the total number of increasing subsequence from i to n of the index.
int count(const vector<int>& nums, int n) {
if (n == 0) return 1;
if (c_[n] > 0) return c_[n]; int total_count = 0;
int l = len(nums, n); // find the number of increasing subsequence which is short than current subsquence.
for (int i = 0; i < n; ++i)
if (nums[n] > nums[i] && len(nums, i) == l-1)
total_count += count(nums, i); if (total_count == 0)
total_count = 1; return c_[n] = total_count;
} // find the max length of increasing subsequence from i to n of the index.
int len(const vector<int>& nums, int n) {
if (n == 0) return 1;
if (l_[n] > 0) return l_[n]; int max_len = 1; for (int i = 0; i < n; ++i)
if (nums[n] > nums[i])
max_len = max(max_len, len(nums, i) + 1); return l_[n] = max_len;
} };
Appraoch #2: Interation. [Java]
class Solution {
public int findNumberOfLIS(int[] nums) {
int n = nums.length;
if (n == 0) return 0; int[] c = new int[n];
int[] l = new int[n]; Arrays.fill(c, 1);
Arrays.fill(l, 1); for (int i = 1; i < n; ++i) {
for (int j = 0; j < i; ++j) {
if (nums[i] > nums[j])
if (l[j] + 1 > l[i]) {
l[i] = l[j] + 1;
c[i] = c[j];
} else if (l[j] + 1 == l[i]){
c[i] += c[j];
}
}
} int max_len = 0;
for (int i = 0; i < n; ++i)
if (l[i] > max_len)
max_len = l[i]; int ans = 0;
for (int i = 0; i < n; ++i) {
if (l[i] == max_len)
ans += c[i];
} return ans;
}
}
Analysis:
The idea is to use two arrays l[n] ans c[n] to record the maximum length os Incresing Subsequence ans the coresponding number of there sequence which ends with nums[i], respectively. That is:
l[i]: the lenght of the Longest Increasing Subseuqence which ends with nums[i].
c[i]: the number of the Longest Increasing Subsequence which ends with nums[i].
Then, the result is the sum of each c[i] while its corresponding l[i] is the maximum length.
Reference:
673. Number of Longest Increasing Subsequence的更多相关文章
- 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 解题报告(Python)
[LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...
- [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. Exampl ...
- 【LeetCode】673. Number of Longest Increasing Subsequence
题目: Given an unsorted array of integers, find the number of longest increasing subsequence. Example ...
- LeetCode 673. 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 最长递增序列的个数
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
- [Swift]LeetCode673. 最长递增子序列的个数 | 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 ...
随机推荐
- Django基础教程
实例练习1-提交数据并展示 1.app_01下的views.py info_list=[] def userInfor(req): if req.method=="POST": u ...
- php 框架选择
背景 很多初级php甚至中级php都会陷入框架选择困难症,要么必须使用什么框架,要么一定不使用什么框架,而对框架的选择带来的效益和负担的成本并不是很清晰 框架大概分为以下这些 1. 简单轻量:tp,c ...
- Rakefile实例教程
一.简介 简单的说Rakefile就是使用Ruby语法的makefile, 对应make的工具就是rake. 在Ruby on Rails里面, 不管是数据库的初始化, 内容初始化, 删除, 还是测试 ...
- sdk接入
文档 http://blog.csdn.net/chenjie19891104/article/details/42217281 视频: https://chuanke.baidu.com/v2869 ...
- mstsc本地驱动器
mstsc 可以把本地驱动器 挂载到服务器上
- Netty 系列(三)Netty 入门
Netty 系列(三)Netty 入门 Netty 是一个提供异步事件驱动的网络应用框架,用以快速开发高性能.高可靠性的网络服务器和客户端程序.更多请参考:Netty Github 和 Netty中文 ...
- C语言基础第二次作业
PTA第一次作业 题目7-1 统计学生成绩 1.实验代码 #include<stdio.h> int main(void){ ,B=,C=,D=,E=,f; scanf("%d ...
- 2018.10.20 bzoj1079: [SCOI2008]着色方案(多维dp)
传送门 dp妙题. f[a][b][c][d][e][last]f[a][b][c][d][e][last]f[a][b][c][d][e][last]表示还剩下aaa个可以用一次的,还剩下bbb个可 ...
- 2018.10.17 NOIP模拟 发电机(概率dp)
传送门 考试空间开大了爆零不然只有30分爆栈? 话说这题真的坑1e7没法写dfsdfsdfs 其实很好推式子. 考虑每个点安一个发动机的概率,推一波式子做个等比数列求和什么的可以证明出来是严格的1si ...
- 2018.09.17 atcoder Digit Sum(数论)
传送门 数论好题啊. 首先对于b<=sqrt(n)b<=sqrt(n)b<=sqrt(n)的情况直接枚举b判断一下就行了. 下面谈一谈如何解决b>sqrt(n)b>sqr ...