-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathK_Empty_Slots.cpp
92 lines (79 loc) · 2.61 KB
/
K_Empty_Slots.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// using BST
class Solution {
public:
int kEmptySlots(vector<int>& flowers, int k) {
set<int> bloom;
for(int i = 0; i < flowers.size(); i++) {
auto it = bloom.insert(flowers[i]).first;
if (it != bloom.begin()) {
if (flowers[i] - *(--it) == k + 1) {
return i + 1;
}
it++;
}
if (++it != bloom.end() and *it - flowers[i] == k + 1) {
return i + 1;
}
}
return -1;
}
};
// using segment tree
class Solution {
vector <int> segmentTree;
int n;
void initSegmentTree(int N) {
int length = 2 * pow(2.0, floor(log((double) N) / log(2.0)) + 1 );
segmentTree.resize(length, 0);
}
int query(int node, int left, int right, const int lower, const int upper) {
if(lower > right or upper < left) {
return 0;
}
if(left >= lower and right <= upper) {
return segmentTree[node];
}
int mid = left + (right - left) / 2;
int leftIndx = node << 1, rightIndx = leftIndx | 1;
return query(leftIndx, left, mid, lower, upper) + query(rightIndx, mid + 1, right, lower, upper);
}
int query(int lower, int upper) {
return query(1, 0, n - 1, lower, upper);
}
int update(int node, int left, int right, const int indx) {
if(indx < left or indx > right or left > right) {
return 0;
}
if(left == right) {
return ++segmentTree[node];
}
int mid = left + (right - left) / 2;
int leftIndx = node << 1, rightIndx = leftIndx | 1;
return segmentTree[node] = update(leftIndx, left, mid, indx) + update(rightIndx, mid + 1, right, indx);
}
void update(int indx) {
update(1, 0, n - 1, indx);
}
public:
int kEmptySlots(vector<int>& flowers, int k) {
n = flowers.size();
initSegmentTree(n);
vector<bool> bloom(n, false);
for(int i = 0; i < n; i++) {
flowers[i]--;
bloom[flowers[i]] = true;
update(flowers[i]);
int left = max(flowers[i] - k, 0);
int right = max(flowers[i] - 1, 0);
if(left > 0 and query(left, right) == 0 and bloom[left - 1]) {
return i + 1;
}
left = min(flowers[i] + 1, n - 1);
right = min(flowers[i] + k, n - 1);
if(right < n - 1 and query(left, right) == 0 and bloom[right + 1]) {
return i + 1;
}
}
return -1;
}
};