Skip to content

Commit ae6553a

Browse files
authored
Update 1793-maximum-score-of-a-good-subarray.js
1 parent 0416c0a commit ae6553a

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

1793-maximum-score-of-a-good-subarray.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,26 @@ const maximumScore = function(nums, k) {
2323

2424
return ans;
2525
};
26+
27+
// another
28+
29+
/**
30+
* @param {number[]} nums
31+
* @param {number} k
32+
* @return {number}
33+
*/
34+
const maximumScore = function(nums, k) {
35+
const n = nums.length, { max, min } = Math
36+
let l = k, r = k, mi = nums[k]
37+
let res = nums[k]
38+
while(l > 0 || r < n - 1) {
39+
if(l === 0) r++
40+
else if(r === n - 1) l--
41+
else if(nums[l - 1] < nums[r + 1]) r++
42+
else l--
43+
mi = min(mi, nums[l], nums[r])
44+
res = max(res, mi * (r - l + 1))
45+
}
46+
47+
return res
48+
};

0 commit comments

Comments
 (0)