<p>You are given an integer array <code>nums</code> and an integer <code>threshold</code>.</p>

<p>Find any subarray of <code>nums</code> of length <code>k</code> such that <strong>every</strong> element in the subarray is <strong>greater</strong> than <code>threshold / k</code>.</p>

<p>Return<em> the <strong>size</strong> of <strong>any</strong> such subarray</em>. If there is no such subarray, return <code>-1</code>.</p>

<p>A <strong>subarray</strong> is a contiguous non-empty sequence of elements within an array.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,3,4,3,1], threshold = 6
<strong>Output:</strong> 3
<strong>Explanation:</strong> The subarray [3,4,3] has a size of 3, and every element is greater than 6 / 3 = 2.
Note that this is the only valid subarray.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [6,5,6,5,8], threshold = 7
<strong>Output:</strong> 1
<strong>Explanation:</strong> The subarray [8] has a size of 1, and 8 &gt; 7 / 1 = 7. So 1 is returned.
Note that the subarray [6,5] has a size of 2, and every element is greater than 7 / 2 = 3.5. 
Similarly, the subarrays [6,5,6], [6,5,6,5], [6,5,6,5,8] also satisfy the given conditions.
Therefore, 2, 3, 4, or 5 may also be returned.</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums[i], threshold &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution:
    def validSubarraySize(self, nums: List[int], threshold: int) -> int:
#         使用单调栈：我们将遍历数组，使用栈来维护一个递增的子数组。对于每一个元素，当发现栈顶元素大于当前元素时，我们将其弹出，并计算以栈顶元素为最小值的子数组的大小。

# 检查条件：每当我们确定了一个以栈顶元素为最小值的子数组后，计算该子数组的大小 k，并检查是否满足 num[i] * k > threshold。



# stack 存储的是索引：stack 保存的是递增子序列中元素的索引，而不是元素本身的值。例如，如果当前的 i 索引处的元素比栈顶元素小，说明我们已经找到了一个子数组，在这个子数组中，栈顶元素是最小值。

# 宽度 w 的计算：

# 当栈为空时，w = i，这表示我们从索引 0 开始到当前的 i，所有元素的最小值是刚刚出栈的元素，因此子数组的宽度为 i。
# 如果栈不为空，则表示栈中还剩下更小的元素。此时我们需要从栈顶（stack[-1]）之后一个元素开始到当前 i 处的子数组，因此宽度为 i - stack[-1] - 1。

        n = len(nums)
        nums.append(0)  # 方便处理数组的最后一个元素
        stack = []
        
        for i in range(n + 1):
            while stack and nums[stack[-1]] > nums[i]:
                h = nums[stack.pop()]
                w = i if not stack else i - stack[-1] - 1
                if h * w > threshold:
                    return w
            stack.append(i)
        
        return -1
                
365