562. Maximum Score of a Good Subarray

HardBinary SearchTwo PointersGreedy

Given an array and an index k, the score of a subarray (i..j) is the minimum element in it multiplied by its length (j - i + 1). A subarray is good if it includes the index k (i <= k <= j). Return the maximum score of a good subarray. Input: '[nums], k'.

Input: '[nums], k'.

Output: Integer — the maximum good-subarray score.

Examples

Example 1
Input: [1,4,3,7,4,5], 3
Output: 15
Explanation: Subarray [4,3,7,4,5] has min 3, length 5.
Example 2
Input: [5,5,4,5,4,1,1,1], 0
Output: 20
Explanation: Best score expanding from index 0.
Example 3
Input: [1], 0
Output: 1
Explanation: Single element.

Constraints

Asked by

GoogleBloomberg
Solve this problem in the editor →