Maximum Coin Collection - Problem

Imagine Mario driving down a bustling two-lane freeway where coins and tolls are scattered every mile! 🏎️💰

You're given two integer arrays lane1 and lane2, where each value represents the coins Mario gains (positive values) or the toll he pays (negative values) at that mile.

The Rules:

  • Mario always starts in lane 1 but can enter the freeway at any mile
  • He can switch lanes at most 2 times during his journey
  • He can switch lanes immediately upon entering or just before exiting
  • He must travel at least one mile before exiting
  • He can exit at any mile after traveling

Goal: Find the maximum number of coins Mario can collect by choosing the optimal entry point, exit point, and lane switching strategy.

Example: If lane1 = [1, -3, 4] and lane2 = [2, 5, -1], Mario could enter at mile 0, switch to lane 2 immediately, collect 2+5=7 coins in miles 0-1, then switch back to lane 1 for mile 2 to collect 4 more coins, totaling 11 coins with exactly 2 switches!

Input & Output

example_1.py — Basic Case
$ Input: lane1 = [1, -3, 4], lane2 = [2, 5, -1]
Output: 11
💡 Note: Mario enters at mile 0, immediately switches to lane 2 (1 switch), collects 2+5=7 coins in miles 0-1, then switches back to lane 1 (2nd switch) to collect 4 coins in mile 2. Total: 7+4=11 coins with exactly 2 switches used.
example_2.py — No Switch Strategy
$ Input: lane1 = [5, 4, 3], lane2 = [1, 1, 1]
Output: 12
💡 Note: Lane 1 has consistently better values, so Mario stays in lane 1 for the entire journey (0 switches), collecting 5+4+3=12 coins. This is better than any switching strategy.
example_3.py — Single Element
$ Input: lane1 = [10], lane2 = [-5]
Output: 10
💡 Note: With only one mile available, Mario must choose the better option. He stays in lane 1 to collect 10 coins rather than switching to lane 2 which would cost him 5 coins.

Constraints

  • 1 ≤ lane1.length, lane2.length ≤ 103
  • lane1.length == lane2.length
  • -103 ≤ lane1[i], lane2[i] ≤ 103
  • Mario can make at most 2 lane switches
  • Mario must travel at least 1 mile

Visualization

Tap to expand
Maximum Coin Collection - DP Solution INPUT Mario's Two-Lane Freeway Lane 1 1 -3 4 Lane 2 2 5 -1 Mile 0 Mile 1 Mile 2 M = Mario Rules: - Start in Lane 1 - Max 2 lane switches - Enter/exit any mile - Travel at least 1 mile lane1 = [1, -3, 4] lane2 = [2, 5, -1] ALGORITHM STEPS 1 Define DP States dp[i][j][k] = max coins at mile i, lane j, k switches 2 Initialize Base Can start at any mile in lane 1 or switch to 2 3 Transition Stay: dp[i][j][k]+lane[i] Switch: dp[i][1-j][k-1] 4 Find Maximum Track max across all states and positions Optimal Path: L1:1 switch L2:5 L2:5 -1 1 + 5 + 5 + (-1) = 10? No! Best: 2 + 5 + 4 = 11 FINAL RESULT Optimal Strategy: L1 L2 2 5 4 switch! M0 M1 M2 Calculation: Mile 0 (L2): +2 Mile 1 (L2): +5 Mile 2 (L1): +4 Total: 2+5+4 = 11 Output: 11 OK - Maximum coins! Key Insight: The DP solution tracks 3 dimensions: position (mile), current lane, and switches used (0, 1, or 2). Entry can be at any mile, and switch can happen immediately. The optimal path here: start in Lane 2, collect 2+5, then switch to Lane 1 at Mile 2 to collect 4, totaling 11 coins with just 1 switch. TutorialsPoint - Maximum Coin Collection | Dynamic Programming Approach
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
52.8K Views
Medium-High Frequency
~25 min Avg. Time
1.3K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen