-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path1793-maximum-score-of-a-good-subarray.js
More file actions
42 lines (37 loc) · 1.06 KB
/
1793-maximum-score-of-a-good-subarray.js
File metadata and controls
42 lines (37 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* 1793. Maximum Score of a Good Subarray
* https://leetcode.com/problems/maximum-score-of-a-good-subarray/
* Difficulty: Hard
*
* 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.
*/
/**
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
var maximumScore = function(nums, k) {
let result = nums[k];
let minimum = nums[k];
let left = k;
let right = k;
const length = nums.length;
while (left > 0 || right < length - 1) {
const leftValue = left > 0 ? nums[left - 1] : 0;
const rightValue = right < length - 1 ? nums[right + 1] : 0;
if (leftValue >= rightValue) {
left--;
minimum = Math.min(minimum, leftValue);
} else {
right++;
minimum = Math.min(minimum, rightValue);
}
result = Math.max(result, minimum * (right - left + 1));
}
return result;
};