-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathLongest Increasing Subsequence.cpp
More file actions
45 lines (39 loc) · 1.2 KB
/
Longest Increasing Subsequence.cpp
File metadata and controls
45 lines (39 loc) · 1.2 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
// TC: O(n^2)
int dpSol(vector<int> &nums) {
vector<int> lis(nums.size(), 1);
int max_len = 1;
for(int i = 0; i < nums.size(); i++) {
// check the LIS of numbers before current
// with which current number can be added
for(int j = 0; j < i; j++) {
// if LIS ending with jth number can create a longer LIS
if(nums[j] < nums[i] && lis[j] + 1 > lis[i])
lis[i] = lis[j] + 1;
max_len = max(max_len, lis[i]);
}
}
return max_len;
}
int lengthOfLIS(vector<int>& nums) {
// return dpSol(nums);
return bSearchSol(nums);
}
};
BINARY SEARCH:
int n=nums.size();
vector<int>lis;
lis.push_back(nums[0]);
for(int i=1;i<n;i++)
{
// need to find right position for nums[i]in lis
int ind=lower_bound(lis.begin(),lis.end(),nums[i])-lis.begin();
if(ind==lis.size())
{
lis.push_back(nums[i]);
}
else
{
lis[ind]=nums[i];
}
}
return lis.size();