Class Solution
java.lang.Object
g2701_2800.s2786_visit_array_positions_to_maximize_score.Solution
2786 - Visit Array Positions to Maximize Score.<p>Medium</p>
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and a positive integer <code>x</code>.</p>
<p>You are <strong>initially</strong> at position <code>0</code> in the array and you can visit other positions according to the following rules:</p>
<ul>
<li>If you are currently in position <code>i</code>, then you can move to <strong>any</strong> position <code>j</code> such that <code>i < j</code>.</li>
<li>For each position <code>i</code> that you visit, you get a score of <code>nums[i]</code>.</li>
<li>If you move from a position <code>i</code> to a position <code>j</code> and the <strong>parities</strong> of <code>nums[i]</code> and <code>nums[j]</code> differ, then you lose a score of <code>x</code>.</li>
</ul>
<p>Return <em>the <strong>maximum</strong> total score you can get</em>.</p>
<p><strong>Note</strong> that initially you have <code>nums[0]</code> points.</p>
<p><strong>Example 1:</strong></p>
<p><strong>Input:</strong> nums = [2,3,6,1,9,2], x = 5</p>
<p><strong>Output:</strong> 13</p>
<p><strong>Explanation:</strong></p>
<p>We can visit the following positions in the array: 0 -> 2 -> 3 -> 4.</p>
<p>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.</p>
<p>The total score will be: 2 + 6 + 1 + 9 - 5 = 13.</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> nums = [2,4,6,8], x = 3</p>
<p><strong>Output:</strong> 20</p>
<p><strong>Explanation:</strong></p>
<p>All the integers in the array have the same parities, so we can visit all of them without losing any score.</p>
<p>The total score is: 2 + 4 + 6 + 8 = 20.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i], x <= 10<sup>6</sup></code></li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
maxScore
public long maxScore(int[] nums, int x)
-