Maximum Score of a Good Subarray

Hard Topics Company Tags

Choose a contiguous subarray that includes the element at index k. Its score is the minimum value in that subarray multiplied by its length. Return the largest possible score. Indices are zero-based.

Example 1:

Input: nums = [2,5,5,1]
k = 1

Output: 10

Explanation: The subarray [5,5] includes index 1 and has score 5 * 2 = 10. Extending left drops the minimum to 2, and extending right drops it to 1, so neither improves the score.

Example 2:

Input: nums = [3,1,3]
k = 1

Output: 3

Explanation: Every valid subarray must include the middle value 1. Taking all three elements gives the largest length and score 1 * 3 = 3.

Example 3:

Input: nums = [7]
k = 0

Output: 7

Explanation: The only subarray contains the single value 7, giving score 7 * 1 = 7.

Constraints:

  • 1 <= nums.length <= 100000
  • 1 <= nums[i] <= 20000
  • 0 <= k < nums.length


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.