Skip to content

Commit fb54fe3

Browse files
🔢 Day 30
1 parent 6e575f6 commit fb54fe3

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
## Week 5 🚧
4949

5050
1. [Maximize Distance to Closest Person](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/563/week-5-october-29th-october-31st/3512/) ➡️ [CPP Solution](Week5/maxDistToClosest.cpp)
51+
2. [Number of Longest Increasing Subsequence](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/563/week-5-october-29th-october-31st/3513/) ➡️ [CPP Solution](Week5/findNumberOfLIS.cpp)
5152

5253
## Other Challenges 💪
5354

Week5/findNumberOfLIS.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
public:
3+
int findNumberOfLIS(vector<int>& nums) {
4+
int n = nums.size();
5+
if(n <= 1) return n;
6+
7+
vector<int> dp(n, 1);
8+
vector<int> count(n, 1);
9+
10+
for(int i = 1; i < n; ++i) {
11+
for(int j = 0; j < i; ++j) {
12+
if(nums[i] > nums[j]) {
13+
if(dp[j] + 1 > dp[i]) {
14+
dp[i] = dp[j] + 1;
15+
count[i] = count[j];
16+
} else if(dp[j] + 1 == dp[i]) {
17+
count[i] += count[j];
18+
}
19+
}
20+
}
21+
}
22+
23+
int lis = *max_element(dp.begin(), dp.end());
24+
int numLis = 0;
25+
26+
for(int i = 0; i < n; ++i) {
27+
if(dp[i] == lis) {
28+
numLis += count[i];
29+
}
30+
}
31+
32+
return numLis;
33+
}
34+
};

0 commit comments

Comments
 (0)