<p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p>

<p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i &lt;= k &lt;= j</code>.</p>

<p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p>

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

<pre>
<strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15. 
</pre>

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

<pre>
<strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0
<strong>Output:</strong> 20
<strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
</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] &lt;= 2 * 10<sup>4</sup></code></li>
	<li><code>0 &lt;= k &lt; nums.length</code></li>
</ul>

"""
___
Goal:
___

return max score of good subarray

___
Examples: 1 @ 0:35, 
___

Input: nums = [1,4,3,7,4,5], k = 3
Output: 15

___
Assumptions:
___


1. score of subarray (i, j) def:
    - min(nums[i], nums[i + 1], ... , nums[j]) * (j - i + 1)
    - computing min * the window_size := j - i + 1

2. good subarray def:
    i <= k <= j
    - i.e. the index k must be in the subarray

3. 1 <= len(nums) <= 10^5
    - Ceiling is O(log(len(nums)len(nums))

4. 1 <= nums[i] <= 20_000

___
Algorithms:
___

Algo 1: Brute Force
T: O(len(nums)^2)
S: O(1)

Algo 2: Monotonic Stacks
T: O(len(nums))
S: O(len(nums))

___
Psuedocode:
___

1. Use Monotonic stacks to efficiently pre-compute:
    1. if k is in a subarray
    2. the window size

2. iterate through 

"""



class Solution:
    def maximumScore(self, nums: List[int], k: int) -> int:
        n = len(nums)

        prefix = [-1] * n  # Store the next lower element from the left.
        suffix = [n] * n  # Store the next lower element from the right.

        left_stack = []  
        right_stack = []
      
        for i in range(n - 1, -1, -1):
            while left_stack and nums[left_stack[-1]] > nums[i]:
                prefix[left_stack.pop()] = i
            left_stack.append(i)

        for i in range(n):
            while right_stack and nums[right_stack[-1]] > nums[i]:
                suffix[right_stack.pop()] = i
            right_stack.append(i)
     
        return max([nums[i] * (suffix[i] - prefix[i] - 1) for i in range(n) if prefix[i] < k < suffix[i]])
        
963