-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path300_Longest_Increasing_Subsequence.cpp
59 lines (56 loc) · 1.32 KB
/
300_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
#include <string>
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <stack>
#include <random>
#include <numeric>
#include <iomanip>
#include <map>
#include <cmath>
#include <cstring>
using namespace std;
class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
// 2. DP + BinarySearch
// O(n logn)
// It works, but hard to recall it in an interview
// Specifically, update an array ('tails') which store a increasing subarray
// Visit all numbers, and
// if one number larger than all numbers in 'tails', then append it at last
// otherwise, insert it into 'tails' by binary search.
if (nums.size() == 0) return 0;
vector<int> tails(1, nums[0]);
for (int i = 1; i < nums.size(); ++i)
{
if (nums[i] > tails.back()) tails.push_back(nums[i]);
else {
int l = 0, r = tails.size() - 1;
while (l < r)
{
int m = (l + r) >> 1;
if (tails[m] < nums[i]) l = m + 1;
else r = m;
}
tails[l] = nums[i];
}
}
return tails.size();
// 1. DP
// O(n^2)
if (nums.size() == 0) return 0;
vector<int> dp(nums.size(), 1);
for (int i = 1; i < nums.size(); ++i) {
for (int j = 0; j < i; ++j) {
if (nums[j] < nums[i])
dp[i] = max(dp[i], dp[j] + 1);
}
}
int maxx = dp[0];
for (auto n : dp)
maxx = max(maxx, n);
return maxx;
}
};