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

r'''
# Requirements

n = len(nums)
1 <= n <= 100,000
0 <= k < n
1 <= nums[i] <= 20,000

# Edge cases

1. n == 1
    - Just one possible subarray
2. k == 0 or k == n - 1
    - When k == 0, i can only be 0
    - When k == n - 1, j can only be n - 1

# Solutions

1. Brute force, try each pair
    - O(n^3) time, O(1) auxiliary space
2. Brute force, improved with prefix and suffix minimum arrays
    - O(n^2) time, O(n) auxiliary space
3. Two pointers, prefix and suffix minimum arrays
    - O(n) time, O(n) auxiliary space

             0 1 2 3 4 5
       nums=[1,4,3,7,4,5]
prefix_mins=       7 4 4
suffix_mins= 1 3 3 7

             i   j 3  4  5
             =============
             0     4  5  6
             1     9 12 15
             2     6  9 12
             3     7  8 12

             0 1 2 3 4 5
       nums=[1,4,3,7,4,5]
prefix_mins=       7 4 4
suffix_mins= 1 3 3 7
                       ^
'''
class Solution:
    def maximumScore(self, nums: List[int], k: int) -> int:
        n = len(nums)

        # Edge case
        if n == 1:
            return nums[0]
        
        # Prepare prefix and suffix minimum arrays
        prefix_mins, suffix_mins = [nums[k]] * n, [nums[k]] * n

        for i in range(k - 1, -1, -1):
            suffix_mins[i] = min(suffix_mins[i + 1], nums[i])
        for j in range(k + 1, n):
            prefix_mins[j] = min(prefix_mins[j - 1], nums[j])

        # Find max score
        max_score, i, j = nums[k], k, k

        def val_at(vals: list[int], pos: int, default: int) -> int:
            return vals[pos] if pos >= 0 and pos < len(vals) else default
        
        while i >= 0 and j < n:
            max_score = max(max_score, min(suffix_mins[i], prefix_mins[j]) * (j - i + 1))

            if val_at(suffix_mins, i - 1, 0) >= val_at(prefix_mins, j + 1, 0):
                i -= 1
            else:
                j += 1

        return max_score
1077