2786. Visit Array Positions to Maximize Score Medium
1/**
2 * [2786] Visit Array Positions to Maximize Score
3 *
4 * You are given a 0-indexed integer array nums and a positive integer x.
5 * You are initially at position 0 in the array and you can visit other positions according to the following rules:
6 *
7 * If you are currently in position i, then you can move to any position j such that i < j.
8 * For each position i that you visit, you get a score of nums[i].
9 * If you move from a position i to a position j and the parities of nums[i] and nums[j] differ, then you lose a score of x.
10 *
11 * Return the maximum total score you can get.
12 * Note that initially you have nums[0] points.
13 *
14 * <strong class="example">Example 1:
15 *
16 * Input: nums = [2,3,6,1,9,2], x = 5
17 * Output: 13
18 * Explanation: We can visit the following positions in the array: 0 -> 2 -> 3 -> 4.
19 * The corresponding values are 2, 6, 1 and 9. Since the integers 6 and 1 have different parities, the move 2 -> 3 will make you lose a score of x = 5.
20 * The total score will be: 2 + 6 + 1 + 9 - 5 = 13.
21 *
22 * <strong class="example">Example 2:
23 *
24 * Input: nums = [2,4,6,8], x = 3
25 * Output: 20
26 * Explanation: All the integers in the array have the same parities, so we can visit all of them without losing any score.
27 * The total score is: 2 + 4 + 6 + 8 = 20.
28 *
29 *
30 * Constraints:
31 *
32 * 2 <= nums.length <= 10^5
33 * 1 <= nums[i], x <= 10^6
34 *
35 */
36pub struct Solution {}
37
38// problem: https://leetcode.com/problems/visit-array-positions-to-maximize-score/
39// discuss: https://leetcode.com/problems/visit-array-positions-to-maximize-score/discuss/?currentPage=1&orderBy=most_votes&query=
40
41// submission codes start here
42
43impl Solution {
44 pub fn max_score(nums: Vec<i32>, x: i32) -> i64 {
45
46 }
47}
48
49// submission codes end
50
51#[cfg(test)]
52mod tests {
53 use super::*;
54
55 #[test]
56 fn test_2786() {
57 }
58}
59
Back
© 2025 bowen.ge All Rights Reserved.