-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path456.cpp
More file actions
64 lines (64 loc) · 1.86 KB
/
Copy path456.cpp
File metadata and controls
64 lines (64 loc) · 1.86 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
class Solution {
// Runtime: 520 ms, faster than 24.35% of C++ online submissions for 132
// Pattern. Memory Usage: 9.9 MB, less than 89.07% of C++ online submissions
// for 132 Pattern.
public:
bool find132pattern(vector<int> &nums) {
if (nums.size() < 3)
return false;
for (auto i = nums[0], j = 1; j + 1 < nums.size(); j++) {
if (i < nums[j]) {
if (find_if(begin(nums) + j + 1, end(nums),
[l = i, m = nums[j]](auto &a) {
return l < a and a < m;
}) != end(nums))
return true;
} else
i = nums[j];
}
return false;
}
};
class Solution2 {
// Runtime: 292 ms, faster than 34.78% of C++ online submissions for 132
// Pattern. Memory Usage: 10.6 MB, less than 33.33% of C++ online submissions
// for 132 Pattern.
public:
bool find132pattern(vector<int> &nums) {
if (nums.size() < 3)
return false;
auto k = vector<int>{next(begin(nums), 2), end(nums)};
sort(begin(k), end(k));
for (auto i = nums[0], j = 1; j + 1 < nums.size(); j++) {
if (i < nums[j]) {
auto it = upper_bound(begin(k), end(k), i);
if (it != end(k) and *it < nums[j])
return true;
} else
i = nums[j];
k.erase(lower_bound(begin(k), end(k), nums[j + 1]));
}
return false;
}
};
class Solution3 {
// Runtime: 24 ms, faster than 88.49% of C++ online submissions for 132
// Pattern. Memory Usage: 10.3 MB, less than 60.77% of C++ online submissions
// for 132 Pattern.
public:
bool find132pattern(vector<int> &nums) {
int s3 = INT_MIN;
stack<int> st;
for (auto it = rbegin(nums); it != rend(nums); ++it) {
if (*it < s3)
return true;
else
while (!st.empty() && *it > st.top()) {
s3 = st.top();
st.pop();
}
st.push(*it);
}
return false;
}
};