<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>

from bisect import bisect_left

def calc_left_smallest_indices(nums):
    increasing_nums = []
    left_first_small_indices = [0] * len(nums)
    for i, num in enumerate(nums):
        if increasing_nums and num > increasing_nums[-1][0]:
            left_first_small_indices[i] = i
        
        while increasing_nums and increasing_nums[-1][0] >= num:
            increasing_nums.pop()
        if increasing_nums:
            left_first_small_indices[i] = max(left_first_small_indices[i], -increasing_nums[-1][1] + 1)
        increasing_nums.append((num, -i))
    return left_first_small_indices


class Solution:
    def validSubarraySize(self, nums: List[int], threshold: int) -> int:
        # get prefix_min, suffix_min
        left_indices = calc_left_smallest_indices(nums)
        right_left_indices = calc_left_smallest_indices(nums[::-1])
        right_indices = [0] * len(nums)
        for i in range(len(nums)-1, -1, -1):
            right_indices[i] = len(nums)-1 - right_left_indices[len(nums)-1-i]
        
        self.nums = nums
        res = -1
        # print(left_indices)
        # print(right_indices)

        for i in range(len(nums)):
            k = right_indices[i] - left_indices[i] + 1
            # k = self.find_largest_subarray(i)
            if k * nums[i] > threshold:
                res = max(k, res)
        return res
    
    def find_largest_subarray(self, i):
        left = 0
        for l in range(i-1,-1, -1):
            if self.nums[l] < self.nums[i]:
                left = l + 1
                break
        right = len(self.nums) - 1
        for r in range(i+1, len(self.nums)):
            if self.nums[r] < self.nums[i]:
                right = r - 1
                break
        return right - left + 1
33100