-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
count-the-number-of-k-big-indices.cpp
60 lines (56 loc) · 1.68 KB
/
count-the-number-of-k-big-indices.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
// Time: O(nlogk)
// Space: O(n)
// heap
class Solution {
public:
int kBigIndices(vector<int>& nums, int k) {
vector<bool> right(size(nums));
priority_queue<int> max_heap1;
for (int i = size(nums) - 1; i >= 0; --i) {
if (size(max_heap1) == k && nums[i] > max_heap1.top()) {
right[i] = true;
}
max_heap1.emplace(nums[i]);
if (size(max_heap1) == k + 1) {
max_heap1.pop();
}
}
int result = 0;
priority_queue<int> max_heap2;
for (int i = 0; i < size(nums); ++i) {
if (size(max_heap2) == k && nums[i] > max_heap2.top() && right[i]) {
++result;
}
max_heap2.emplace(nums[i]);
if (size(max_heap2) == k + 1) {
max_heap2.pop();
}
}
return result;
}
};
// Time: O(nlogn)
// Space: O(n)
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
// ordered set
class Solution2 {
public:
int kBigIndices(vector<int>& nums, int k) {
using ordered_set = tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag, tree_order_statistics_node_update>;
ordered_set os1, os2;
for (int i = 0; i < size(nums); ++i) {
os2.insert({nums[i], i});
}
int result = 0;
for (int i = 0; i < size(nums); ++i) {
os2.erase({nums[i], i});
if (os1.order_of_key({nums[i], 0}) >= k && os2.order_of_key({nums[i], 0}) >= k) {
++result;
}
os1.insert({nums[i], i});
}
return result;
}
};