File tree 2 files changed +35
-0
lines changed
2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change 48
48
## Week 5 🚧
49
49
50
50
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 )
51
52
52
53
## Other Challenges 💪
53
54
Original file line number Diff line number Diff line change
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
+ };
You can’t perform that action at this time.
0 commit comments