-
Notifications
You must be signed in to change notification settings - Fork 0
/
longest_increasing_subsequence.cpp
69 lines (58 loc) · 1.64 KB
/
longest_increasing_subsequence.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// https://leetcode.com/problems/longest-increasing-subsequence/
// May 09, 2016
#include <iostream>
#include <vector>
#include <limits>
using namespace std;
class Solution {
public:
int lengthOfLIS(vector<int>& nums)
{
// This stores length of longest increasing subsequence till that index
vector<int> vecLengthLIS;
vecLengthLIS.reserve(nums.size());
// initializing with zeros
for (unsigned int i=0; i != nums.size(); ++i)
{
vecLengthLIS.push_back(0);
}
for (unsigned int i=0; i != nums.size(); ++i)
{
// check the values in [0,...,i-1]
int max_length = 0;
for (unsigned int j=0; j != i; ++j)
{
if (nums[j] < nums[i])
{
if (vecLengthLIS[j] > max_length)
{
max_length = vecLengthLIS[j];
}
}
}
vecLengthLIS[i] = 1 + max_length;
}
int length_LIS = 0;
for (unsigned int i=0; i != nums.size(); ++i)
{
if (length_LIS < vecLengthLIS[i])
{
length_LIS = vecLengthLIS[i];
}
}
return length_LIS;
}
};
int main(int argc, char* argv[])
{
vector<int> vec = {10, 9, 2, 5, 3, 7, 101, 18};
Solution sln;
int length_LIS = sln.lengthOfLIS(vec);
cout << "length(LIS): " << length_LIS << endl;
return 0;
}
/*
Current solution's time complexity: O(n^2)
TBD: Improve to O(n log(n))
http://www.geeksforgeeks.org/longest-monotonically-increasing-subsequence-size-n-log-n/
*/