> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Maximum Score of a Good Subarray

> Tested Python solution for LeetCode 1793 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 1793, [Hard](/catalog/hard). Topics: [Array](/catalog/topics/array), [Two Pointers](/catalog/topics/two-pointers), [Binary Search](/catalog/topics/binary-search), [Stack](/catalog/topics/stack), [Monotonic Stack](/catalog/topics/monotonic-stack), Cartesian Tree. [View on LeetCode](https://leetcode.com/problems/maximum-score-of-a-good-subarray/description/).

Generate this problem as a practice environment: tested reference solution, 20 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1793   # by problem number
lcpy gen -s maximum_score_of_a_good_subarray   # by problem name
```

## Problem

You are given an array of integers `nums` (0-indexed) and an integer `k`.

The **score** of a subarray `(i, j)` is defined as `min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)`. A **good** subarray is a subarray where `i <= k <= j`.

Return the maximum possible score of a good subarray.

### Examples

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

```
Input: nums = [5,5,4,5,4,1,1,1], k = 0
Output: 20
Explanation: The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
```

### Constraints

* `1 <= nums.length <= 10^5`
* `1 <= nums[i] <= 2 * 10^4`
* `0 <= k < nums.length`

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/maximum_score_of_a_good_subarray/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/maximum_score_of_a_good_subarray/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def maximum_score(self, nums: list[int], k: int) -> int:
        left = right = k
        cur_min = nums[k]
        best = cur_min
        while left > 0 or right < len(nums) - 1:
            next_left = nums[left - 1] if left > 0 else 0
            next_right = nums[right + 1] if right < len(nums) - 1 else 0
            if next_left >= next_right:
                left -= 1
            else:
                right += 1
            cur_min = min(cur_min, max(next_left, next_right))
            best = max(best, cur_min * (right - left + 1))
        return best
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.