-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongest_Inc_subseq.cpp
More file actions
50 lines (46 loc) · 1008 Bytes
/
Longest_Inc_subseq.cpp
File metadata and controls
50 lines (46 loc) · 1008 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <bits/stdc++.h>
using namespace std;
int maxIncSubSeqSum(vector<int> v)
{
const int n = v.size();
vector<int> dp(n);
for (int i = 0; i < n; i++)
{
dp[i] = v[i];
}
for (int i = 1; i < n; i++)
{
for (int j = 0; j < i; j++)
{
if (v[i] > v[j] and dp[i] < dp[j] + v[i])
dp[i] = dp[j] + v[i];
}
}
return *max_element(dp.begin(), dp.end());
}
int LongestIncSubSeq(vector<int> v)
{
vector<int> temp;
temp.push_back(v[0]);
//n(logn) solution
for (int i = 1; i < v.size(); i++)
{
vector<int>::iterator LB = lower_bound(temp.begin(), temp.end(), v[i]);
if (LB == temp.end())
{
temp.push_back(v[i]);
}
else
{
*LB = v[i];
}
}
return temp.size();
}
int main()
{
using namespace std;
vector<int> v = {1, 2, 3, 100};
std::cout << maxIncSubSeqSum(v) << " " << LongestIncSubSeq(v);
return 0;
}