-
Notifications
You must be signed in to change notification settings - Fork 3
/
2334.cpp
41 lines (35 loc) · 1.17 KB
/
2334.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
class Solution {
public:
int validSubarraySize(vector<int>& nums, int threshold) {
int n = nums.size();
vector<int> prevSmaller(n, -1);
vector<int> nextSmaller(n, -1);
stack<int> st;
// next smaller
// monotonic increase (has duplicate)
for (int i = 0; i < n; ++i) {
while (!st.empty() && nums[i] < nums[st.top()]) {
nextSmaller[st.top()] = i;
st.pop();
}
st.push(i);
}
while (!st.empty()) st.pop();
// prev smaller
// monotonic increase (has duplicate)
for (int i = n - 1; i >= 0; --i) {
while (!st.empty() && nums[i] < nums[st.top()]) {
prevSmaller[st.top()] = i;
st.pop();
}
st.push(i);
}
for (int i = 0; i < n; ++i) {
int left = prevSmaller[i];
int right = nextSmaller[i] == -1 ? n : nextSmaller[i];
int length = right - left - 1;
if ((long long)nums[i] * length > (long long)threshold) return length;
}
return -1;
}
};