FastPrepMaximum Non-Adjacent Coins in Two Rows

Maximum Non-Adjacent Coins in Two Rows

Kotak Mahindra Bank logoKotak Mahindra Bank● HardFULLTIMEONSITE INTERVIEW
Learn

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[][]) → long

Examples

Example 1

coins = [[4,5,6,7,8],[1,9,2,10,3]]return = 19

Choose value 9 from column 1 and value 10 from column 3.

Example 2

coins = [[-5,-2],[-3,-4]]return = 0

Skipping every negative coin is optimal.

Constraints

  • coins.length == 2
  • 1 <= 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.

More Kotak Mahindra Bank problems

See Kotak Mahindra Bank hiring insights
public long maxNonAdjacentCoins(int[][] coins) {
    // Write your code here.
}
coins[[4,5,6,7,8],[1,9,2,10,3]]
expected19
Checking account…