-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindNumberOfLIS.cpp
34 lines (29 loc) · 888 Bytes
/
findNumberOfLIS.cpp
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
class Solution {
public:
int findNumberOfLIS(vector<int>& nums) {
int n = nums.size();
if(n <= 1) return n;
vector<int> dp(n, 1);
vector<int> count(n, 1);
for(int i = 1; i < n; ++i) {
for(int j = 0; j < i; ++j) {
if(nums[i] > nums[j]) {
if(dp[j] + 1 > dp[i]) {
dp[i] = dp[j] + 1;
count[i] = count[j];
} else if(dp[j] + 1 == dp[i]) {
count[i] += count[j];
}
}
}
}
int lis = *max_element(dp.begin(), dp.end());
int numLis = 0;
for(int i = 0; i < n; ++i) {
if(dp[i] == lis) {
numLis += count[i];
}
}
return numLis;
}
};