-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution2.cpp
52 lines (48 loc) · 1.22 KB
/
Solution2.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
class BinaryIndexedTree {
public:
BinaryIndexedTree(int n) {
this->n = n;
this->c = vector<int>(n + 1, 0);
}
void update(int x, int val) {
while (x <= n) {
c[x] += val;
x += x & -x;
}
}
int query(int x) {
int s = 0;
while (x > 0) {
s += c[x];
x -= x & -x;
}
return s;
}
private:
int n;
vector<int> c;
};
class Solution {
public:
bool find132pattern(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 n = nums.size();
int left[n + 1];
memset(left, 63, sizeof(left));
for (int i = 0; i < n; ++i) {
left[i + 1] = min(left[i], nums[i]);
}
for (int i = nums.size() - 1; ~i; --i) {
int x = lower_bound(s.begin(), s.end(), nums[i]) - s.begin() + 1;
int y = lower_bound(s.begin(), s.end(), left[i]) - s.begin() + 1;
if (x > y && tree.query(x - 1) > tree.query(y)) {
return true;
}
tree.update(x, 1);
}
return false;
}
};