-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestIncreasingSubsequence.cpp
More file actions
42 lines (39 loc) · 1.04 KB
/
LongestIncreasingSubsequence.cpp
File metadata and controls
42 lines (39 loc) · 1.04 KB
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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution
{
public:
int lengthOfLIS(vector<int> &nums)
{
vector<int> dp(nums.size());
if (nums.size() == 1)
{
return 1;
}
if (adjacent_find(nums.begin(), nums.end(), not_equal_to<>()) == nums.end())
{
return 1;
}
for (int i = 0; i < nums.size(); ++i)
{
dp[i] = 1; // this is the base case before every if condition comparison between the current element at i and any previous number if any to see if it the nums at j can extent to nums at i or not
for (int j = 0; j < i; ++j)
{
if (nums[j] < nums[i])
{
dp[i] = max(dp[i], dp[j] + 1);
}
}
}
return *max_element(dp.begin(), dp.end());
}
};
int main()
{
Solution sol;
vector<int> nums = {2, 5, 3, 7};
cout << sol.lengthOfLIS(nums); // 3 as of the array : [2,5,7]
return 0;
}