Skip to content

Commit a166a70

Browse files
Create numberOfLIS.cpp
1 parent db8ce78 commit a166a70

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

numberOfLIS.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public:
3+
int findNumberOfLIS(vector<int>& nums) {
4+
int n = nums.size();
5+
vector <int> len(n, 1), cnt(n, 1);
6+
int lis = 1;
7+
for(int i = 1; i < n; i++){
8+
for(int j = 0; j < i; j++){
9+
if(nums[i] > nums[j]){
10+
if(len[j] + 1 > len[i]){
11+
len[i] = len[j] + 1;
12+
cnt[i] = cnt[j];
13+
}
14+
else if(len[j] + 1 == len[i]){
15+
cnt[i] += cnt[j];
16+
}
17+
}
18+
lis = max(lis, len[i]);
19+
}
20+
}
21+
int ans = 0;
22+
for(int i = 0; i < n; i++){
23+
if(len[i] == lis)ans += cnt[i];
24+
}
25+
return ans;
26+
}
27+
};

0 commit comments

Comments
 (0)