Skip to content

Commit b0f631b

Browse files
authored
Create number-of-longest-increasing-subsequence.cpp
1 parent 9bf8377 commit b0f631b

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Time: O(n^2)
2+
// Space: O(n)
3+
4+
class Solution {
5+
public:
6+
int findNumberOfLIS(vector<int>& nums) {
7+
auto result = 0, max_len = 0;
8+
vector<pair<int, int>> dp(nums.size(), {1, 1}); // {length, number} pair
9+
for (int i = 0; i < nums.size(); ++i) {
10+
for (int j = 0; j < i; ++j) {
11+
if (nums[i] > nums[j]) {
12+
if (dp[i].first == dp[j].first + 1) {
13+
dp[i].second += dp[j].second;
14+
} else if (dp[i].first < dp[j].first + 1) {
15+
dp[i] = {dp[j].first + 1, dp[j].second};
16+
}
17+
}
18+
}
19+
if (max_len == dp[i].first) {
20+
result += dp[i].second;
21+
} else if (max_len < dp[i].first) {
22+
max_len = dp[i].first;
23+
result = dp[i].second;
24+
}
25+
}
26+
return result;
27+
}
28+
};

0 commit comments

Comments
 (0)