-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathminimum-stability-factor-of-array.cpp
More file actions
63 lines (57 loc) · 1.95 KB
/
minimum-stability-factor-of-array.cpp
File metadata and controls
63 lines (57 loc) · 1.95 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
// Time: O(nlogn * logr)
// Space: O(nlogn)
// number theory, binary search, rmq, sparse table, greedy
class Solution {
public:
int minStable(vector<int>& nums, int maxC) {
const auto& binary_search_right = [&](int left, int right, const auto& check) {
while (left <= right) {
const int mid = left + (right - left) / 2;
if (!check(mid)) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return right;
};
SparseTable rmq(nums, gcd<int, int>);
const auto& check = [&](int l) {
int cnt = 0;
for (int i = 0; i + l - 1 < size(nums);) {
if (rmq.query(i, i + l - 1) >= 2) {
++cnt;
i += l;
} else {
++i;
}
}
return cnt > maxC;
};
return binary_search_right(1, size(nums), check);
}
private:
// Reference: https://cp-algorithms.com/data_structures/sparse-table.html
class SparseTable {
public:
SparseTable(const vector<int>& arr, function<int (int, int)> fn)
: fn(fn) { // Time: O(nlogn) * O(fn) = O(nlogn * logr), Space: O(nlogn)
const int n = size(arr);
const int k = __lg(n);
st.assign(k + 1, vector<int64_t>(n));
st[0].assign(cbegin(arr), cend(arr));
for (int i = 1; i <= k; ++i) {
for (int j = 0; j + (1 << i) <= n; ++j) {
st[i][j] = fn(st[i - 1][j], st[i - 1][j + (1 << (i - 1))]);
}
}
}
int64_t query(int L, int R) const {
const int i = __lg(R - L + 1);
return fn(st[i][L], st[i][R - (1 << i) + 1]); // Time: O(fn) = O(logr)
}
private:
vector<vector<int64_t>> st;
const function<int (int, int)>& fn;
};
};