-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumber_of_Longest_Increasing_Subsequence.cpp
More file actions
62 lines (49 loc) · 1.4 KB
/
Copy pathNumber_of_Longest_Increasing_Subsequence.cpp
File metadata and controls
62 lines (49 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//https://leetcode.com/problems/number-of-longest-increasing-subsequence/
#include <bits/stdc++.h>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
#define maxn 2001
vector<int> nums = {1,2,4,3,5,4,7,2};//{1,3,5,4,7};//{1,2,4,3,5,4,7,2};
int main() {
vector<int> arr;
vector<int>::iterator it;
int len[maxn];
int cnt[maxn];// with arr[i] being end element, how many sequence
arr.emplace_back(nums[0]);
len[0] = 1;cnt[0] = 1;
for(int i=1;i<nums.size();i++){
if(nums[i]>arr[arr.size()-1]){
arr.emplace_back(nums[i]);
len[i] = arr.size();
}else{
it = (lower_bound(arr.begin(), arr.end(), nums[i]));
*it = nums[i];
len[i] = it-arr.begin()+1;
}
int tmp = 0;
if(len[i]!=1){
for(int j=0;j<i;j++){
if((nums[i] > nums[j]) && len[j]==len[i]-1){
tmp += cnt[j];
}
}
cnt[i] = tmp;
}else{
cnt[i] = 1;
}
}
int ret = 0;
int maxlen = 0;
for(int i=0;i<nums.size();i++){
if(maxlen < len[i]) {
maxlen = len[i];
ret = cnt[i];
}else if(maxlen == len[i]){
ret += cnt[i];
}
}
cout<<ret<<endl;
return 0;
}