Problem 36: Longest Increasing Subsequence
Problem Statement: Given an array of n integers, find the length of the longest strictly increasing subsequence.
Input
The first line contains an integer n. The second line contains n space-separated integers.
Output
Print the length of the longest increasing subsequence.
Examples
Input:
8
10 9 2 5 3 7 101 18
Output:
4
Solution in C++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n; cin >> n;
vector<int> a(n);
for(int i = 0; i < n; i++) cin >> a[i];
vector<int> lis;
for (int x : a) {
auto it = lower_bound(lis.begin(), lis.end(), x);
if(it == lis.end()) {
lis.push_back(x);
} else {
*it = x;
}
}
cout << lis.size() << "\n";
return 0;
}