Maximum Non-Adjacent Coins in Two Rows
Problem statement
You are given a 2 × n integer grid of coin values. Choose any set of cells with maximum total value.
No two chosen cells may be horizontally, vertically, or diagonally adjacent. Choosing no cells is allowed and has total value 0.
Function
maxNonAdjacentCoins(coins: int[][]) → longExamples
Example 1
coins = [[4,5,6,7,8],[1,9,2,10,3]]return = 19Choose value 9 from column 1 and value 10 from column 3.
Example 2
coins = [[-5,-2],[-3,-4]]return = 0Skipping every negative coin is optimal.
Constraints
coins.length == 21 <= n == coins[0].length == coins[1].length <= 200000-10^9 <= coins[row][column] <= 10^9- The answer fits in a signed 64-bit integer.
Source note: Interview prompt excerpt preserved as source evidence.