502. Maximum Coin Collection
Alice is driving on a two-lane highway with coins placed at each mile marker. You are provided with two integer arrays, lane1 and lane2, where the value at index i indicates the number of coins she gains or loses at mile i in that particular lane.
- If Alice is in lane 1 at mile i and the value of lane1[i] is positive, she collects that many coins from lane1[i].
- If Alice is in lane 1 at mile i and the value of lane1[i] is negative, she pays a toll and loses the absolute value of lane1[i] coins.
- The same rules apply for lane 2.
Alice may enter the highway at any mile and exit at any later mile after traveling at least one mile. She always starts in lane 1, but she may change lanes at most two times.
A lane change occurs when Alice moves from lane 1 to lane 2 or from lane 2 back to lane 1.
Return the maximum number of coins Alice can collect with at most two lane changes.
Note: Alice may switch lanes immediately after entering or just before leaving the highway.
Example 1:
Input:Â lane1 = [-5,-4,-3], lane2 = [-1,2,3]
Output:Â 5
Explanation:
Alice enters at mile 1 and immediately switches to lane 2. He stays here the entire way.
He collects a total of 2 + 3 = 5 coins.
Example 2:
Input: lane1 = [10, -20, 10], lane2 = [-20, 50, -20]
Output: 70
Explanation:
Alice starts at Mile 0 in lane 1. At Mile 1, she switches to lane 2 to grab the big reward. At Mile 2, she switches back to lane 1 to avoid the penalty in lane 2. She collects 10 + 50 + 10 = 70 coins.
Now Your Turn!
Pick the correct output for the given inputInput:Â lane1 = [1,-2,-10,3], lane2 = [-5,10,0,1]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= lane1.length == lane2.length <= 105
- -109 <= lane1[i], lane2[i] <= 109