-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIncreasing_Subsequence.cpp
More file actions
31 lines (26 loc) · 969 Bytes
/
Increasing_Subsequence.cpp
File metadata and controls
31 lines (26 loc) · 969 Bytes
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 <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> arr(n);
for(int i = 0; i < n; i++) {
cin >> arr[i];
}
// dp[i] stores smallest value that can end an increasing subsequence of length i
vector<int> dp(n + 2);
dp[0] = INT_MIN;
fill(dp.begin() + 1, dp.end(), INT_MAX);
for(int i = 0; i < n; i++) {
// Find the position where current element should be inserted to maintain increasing order
int x = upper_bound(dp.begin(), dp.end(), arr[i]) - dp.begin();
// If current element can create a better increasing subsequence of length x, update dp
if(dp[x-1] < arr[i] && arr[i] < dp[x]) {
dp[x] = arr[i];
}
}
// First infinity position minus 1 gives the length of longest increasing subsequence
int ans = lower_bound(dp.begin(), dp.end(), INT_MAX) - dp.begin() - 1;
cout << ans << endl;
return 0;
}