-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLongest Increasing Subsequence.cpp
More file actions
50 lines (48 loc) · 1.71 KB
/
Longest Increasing Subsequence.cpp
File metadata and controls
50 lines (48 loc) · 1.71 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class Solution {
private:
vector<vector<int>> memo;
int findLIS(int previousIndex, int currentIndex, vector<int>& nums){
if (currentIndex == nums.size()){
return 0;
}
if(memo[previousIndex + 1][currentIndex] != -1) {
return memo[previousIndex + 1][currentIndex];
}
int include = 0;
if (previousIndex == -1 || nums[previousIndex] < nums[currentIndex]) {
include = 1 + findLIS(currentIndex, currentIndex + 1, nums);
}
// During the recursion, previousIndex can range from -1 (no elements included) to n-1 (last element in the array), where n is the size of the input array nums.
// Option 2: Exclude the current element and move to the next
int exclude = findLIS(previousIndex, currentIndex + 1, nums);
// Store the maximum of include and exclude in the memo table
memo[previousIndex + 1][currentIndex] = max(include, exclude);
return memo[previousIndex + 1][currentIndex];
}
public:
int lengthOfLIS(vector<int>& nums) {
int n = nums.size();
memo = vector<vector<int>>(n+1 , vector<int>(n+1, -1));
return findLIS(-1, 0, nums);
}
};// TABULATION
class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
int n = nums.size();
vector<int> dp(n+1, 1);
dp[0] = 1;
int max_lis = 1;
for(int i = 1; i < n; i++){
for(int j = 0; j < i; j++){
int exclude = dp[i];
int include = dp[j] + 1;
if( nums[j] < nums[i]){
dp[i] = max(exclude,include);
}
}
max_lis = max(max_lis,dp[i]);
}
return max_lis;
}
};