Skip to content

Commit 87b93eb

Browse files
authored
Create subarray-with-elements-greater-than-varying-threshold.cpp
1 parent e171922 commit 87b93eb

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Time: O(n)
2+
// Space: O(n)
3+
4+
// mono stack
5+
class Solution {
6+
public:
7+
int validSubarraySize(vector<int>& nums, int threshold) {
8+
vector<int> stk = {-1};
9+
for (int i = 0; i <= size(nums); ++i) {
10+
while (stk.back() != -1 && (i == size(nums) || nums[stk.back()] >= nums[i])) {
11+
int j = stk.back(); stk.pop_back();
12+
if (nums[j] * ((i - 1) - stk.back()) > threshold) {
13+
return (i - 1) - stk.back();
14+
}
15+
}
16+
stk.emplace_back(i);
17+
}
18+
return -1;
19+
}
20+
};

0 commit comments

Comments
 (0)