-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.cpp
44 lines (40 loc) · 965 Bytes
/
Solution.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
class BinaryIndexedTree {
public:
BinaryIndexedTree(int _n)
: n(_n)
, c(_n + 1) {}
void update(int x, int v) {
while (x <= n) {
c[x] = max(c[x], v);
x += x & -x;
}
}
int query(int x) {
int mx = 0;
while (x) {
mx = max(mx, c[x]);
x -= x & -x;
}
return mx;
}
private:
int n;
vector<int> c;
};
class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
vector<int> s = nums;
sort(s.begin(), s.end());
s.erase(unique(s.begin(), s.end()), s.end());
BinaryIndexedTree tree(s.size());
int ans = 1;
for (int x : nums) {
x = lower_bound(s.begin(), s.end(), x) - s.begin() + 1;
int t = tree.query(x - 1) + 1;
ans = max(ans, t);
tree.update(x, t);
}
return ans;
}
};