Leetcode Problem 2334. Subarray With Elements Greater Than Varying Threshold

2334. Subarray With Elements Greater Than Varying Threshold

Leetcode Solutions

C++ O(N) Solution using Monotonic Stacks

  1. Initialize two arrays nextS and prevS to store the indices of the next and previous smaller elements for each index in nums.\n2. Traverse nums from left to right to fill nextS using a stack to keep track of indices with decreasing values.\n3. Traverse nums from right to left to fill prevS similarly.\n4. Iterate over nums and for each element, calculate the maximum subarray size len using nextS and prevS.\n5. If the current element is greater than threshold / len, return len as the result.\n6. If no valid subarray is found, return -1.
UML Thumbnail

Divide and Conquer + Segment Tree

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...