forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
38 lines (34 loc) · 1018 Bytes
/
Solution.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
using pii = pair<int, int>;
class Solution {
public:
vector<int> p;
vector<int> size;
int validSubarraySize(vector<int>& nums, int threshold) {
int n = nums.size();
p.resize(n);
for (int i = 0; i < n; ++i) p[i] = i;
size.assign(n, 1);
vector<pii> arr(n);
for (int i = 0; i < n; ++i) arr[i] = {nums[i], i};
sort(arr.begin(), arr.end());
vector<bool> vis(n);
for (int j = n - 1; ~j; --j) {
int v = arr[j].first, i = arr[j].second;
if (i && vis[i - 1]) merge(i, i - 1);
if (j < n - 1 && vis[i + 1]) merge(i, i + 1);
if (v > threshold / size[find(i)]) return size[find(i)];
vis[i] = true;
}
return -1;
}
int find(int x) {
if (p[x] != x) p[x] = find(p[x]);
return p[x];
}
void merge(int a, int b) {
int pa = find(a), pb = find(b);
if (pa == pb) return;
p[pa] = pb;
size[pb] += size[pa];
}
};