Skip to content

Commit 98ca324

Browse files
authored
Create 2786-visit-array-positions-to-maximize-score.js
1 parent dab2ec3 commit 98ca324

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} x
4+
* @return {number}
5+
*/
6+
const maxScore = function(nums, x) {
7+
let even = nums[0] + (nums[0] % 2 ? -x : 0)
8+
let odd = nums[0] + (nums[0] % 2 ? 0 : -x)
9+
10+
const n = nums.length, { max } = Math
11+
for(let i = 1; i < n; i++) {
12+
const e = nums[i]
13+
if(e % 2 === 1) {
14+
odd = max(even - x, odd) + e
15+
} else {
16+
even = max(even, odd - x) + e
17+
}
18+
}
19+
20+
return max(even, odd)
21+
};

0 commit comments

Comments
 (0)