Visit Array Positions to Maximize Score - Problem
Visit Array Positions to Maximize Score
Imagine you're playing a strategic game where you move through an array from left to right, collecting points along the way. You start at position 0 and can jump to any position ahead of you, but there's a catch - switching between even and odd numbers costs you points!
The Rules:
• Start at position
• You can jump from position
• Each position you visit adds
• Penalty: If you jump from an even number to an odd number (or vice versa), you lose
Goal: Find the maximum total score you can achieve by strategically choosing which positions to visit.
Example: With
Imagine you're playing a strategic game where you move through an array from left to right, collecting points along the way. You start at position 0 and can jump to any position ahead of you, but there's a catch - switching between even and odd numbers costs you points!
The Rules:
• Start at position
0 and collect nums[0] points• You can jump from position
i to any position j where i < j• Each position you visit adds
nums[j] to your score• Penalty: If you jump from an even number to an odd number (or vice versa), you lose
x pointsGoal: Find the maximum total score you can achieve by strategically choosing which positions to visit.
Example: With
nums = [2,3,6,1,9,2] and x = 5, you might visit positions with values [2,6,9] (staying mostly with even numbers to avoid penalties) for a total score of 2+6+9 = 17. Input & Output
example_1.py — Basic Case
$
Input:
nums = [2,3,6,1,9,2], x = 5
›
Output:
13
💡 Note:
Visit positions with values [2,6,9]. Score = 2+6+9-5 = 12. Wait, let me recalculate: We can visit [2,3,6,9] = 2+3+6+9-5 = 15, or [2,6,9] = 2+6+9 = 17, but we pay penalty 2->6 (even to even, no penalty), 6->9 (even to odd, -5 penalty) = 17-5 = 12. Actually optimal is visiting all: 2+3-5+6-5+1+9-5+2 = 8. The optimal path is [2,6,9,2] = 2+6+9+2-5 = 14. Let me recalculate properly: visiting [2,3,9,2] gives 2+3+9+2-5-5 = 6. The answer should be 13 by visiting strategic positions.
example_2.py — High Penalty
$
Input:
nums = [2,4,6,8], x = 3
›
Output:
20
💡 Note:
All numbers are even, so no penalties. Visit all positions: 2+4+6+8 = 20.
example_3.py — Single Element
$
Input:
nums = [5], x = 2
›
Output:
5
💡 Note:
Only one element, so the answer is nums[0] = 5.
Visualization
Tap to expand
Understanding the Visualization
1
Initialize Journey
Start at first cave, determine if it contains blue or red gems
2
Track Best Paths
Maintain the best treasure haul for paths ending at blue caves and red caves
3
Make Optimal Choices
At each cave, decide whether to continue current gem type or switch (paying equipment cost)
4
Final Selection
Choose the better of the two final treasure amounts
Key Takeaway
🎯 Key Insight: Instead of exploring every possible path, we only need to track the maximum treasure for blue-ending and red-ending journeys at each step, making optimal local decisions that lead to the global optimum.
Time & Space Complexity
Time Complexity
O(n)
Single pass through array, constant work per element
✓ Linear Growth
Space Complexity
O(1)
Only storing two variables for even/odd maximum scores
✓ Linear Space
Constraints
- 1 ≤ nums.length ≤ 105
- 1 ≤ nums[i], x ≤ 106
- nums[0] is always included in the final score
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code