Problem · Dynamic Programming
Timed Coin Collection in a 2×N Grid
Learn this problemProblem statement
Start at the top-left cell of a 2 by n grid at time zero. Move each second to an orthogonally adjacent unvisited cell until every cell has been visited exactly once. Visiting cell (r,c) at time t earns t * coins[r][c]. Return the maximum total.
Function
maxTimedCoins(coins: int[][]) → longExamples
Example 1
coins = [[1,4,3,2],[2,1,3,2]]return = 77An optimal route visits top-left, traverses the bottom row to the right, then returns along the top row.
Example 2
coins = [[1,3],[4,2]]return = 19The route top-left, bottom-left, bottom-right, top-right earns 0+4+4+9.
Constraints
1 <= n <= 1000001 <= coins[r][c] <= 100000