forked from yubinbai/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
62 lines (58 loc) · 1.48 KB
/
main.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
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
int n = nums.size();
if (n == 0) {
return 0;
}
// memo[j] — stores the position k of the smallest value X[k]
// such that there is an increasing subsequence of length j
// ending at X[k] on the range k ≤ i
vector<int> memo = {0};
// prev[k] — stores the position of the predecessor of X[k] in the longest increasing
// subsequence ending at X[k].
vector<int> prev(n, -1);
for (int i = 1; i < n; i++) {
if (nums[memo.back()] < nums[i]) {
prev[i] = memo.back();
memo.push_back(i);
} else {
// binary search to update memo
int low = 0, high = memo.size() - 1;
while (low < high) {
int mid = low + (high - low) / 2;
if (nums[memo[mid]] < nums[i]) {
low = mid + 1;
} else {
high = mid;
}
}
if (nums[i] < nums[memo[low]]) {
if (low > 0) {
prev[i] = memo[low - 1];
}
memo[low] = i;
}
}
}
return memo.size();
}
};
int main() {
Solution sol;
vector<int> a = {1, 3, 2, 5, 10};
cout << sol.lengthOfLIS(a) << endl;
vector<int> b = {1, 2, 3, 4, 5};
cout << sol.lengthOfLIS(b) << endl;
return 0;
}