forked from DevAffan/AffiCodes-Hacktoberfest2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LIS.cpp
31 lines (26 loc) · 1014 Bytes
/
LIS.cpp
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
#include <iostream>
#include <vector>
#include <algorithm> // for std::lower_bound
using namespace std;
int longestIncreasingSubsequence(const vector<int>& nums) {
vector<int> lis; // This will store the smallest tail for all increasing subsequences
for (int num : nums) {
// Use binary search to find the index of the smallest number >= num
auto it = lower_bound(lis.begin(), lis.end(), num);
// If num is larger than all elements in lis, it extends the largest subsequence
if (it == lis.end()) {
lis.push_back(num);
} else {
// Otherwise, replace the found index with num
*it = num;
}
}
// The size of lis will be the length of the longest increasing subsequence
return lis.size();
}
int main() {
vector<int> nums = {10, 9, 2, 5, 3, 7, 101, 18};
int length = longestIncreasingSubsequence(nums);
cout << "Length of Longest Increasing Subsequence: " << length << endl;
return 0;
}