FastPrepCan the Current Player Win Babylon?

Can the Current Player Win Babylon?

Google logoGoogle● HardNEW GRADPHONE SCREEN
Learn

Problem statement

A Babylon game state contains several tile piles. heights[i] is the height of pile i, and topColors[i] is its top color. Two players alternate moves.

On a move, choose two different piles and place one whole pile on top of the other. The move is legal when the two piles have the same height or the same top color. The merged pile's height is the sum of both heights, and its top color is the top color of the pile placed on top.

A player who has no legal move loses. Assuming optimal play from both players, return whether the current player can force a win.

Function

canCurrentPlayerWin(heights: int[], topColors: String[]) → boolean

Examples

Example 1

heights = [1,1]topColors = ["R","B"]return = true

The current player merges the equal-height piles and leaves no move.

Example 2

heights = [1,2]topColors = ["R","B"]return = false

The two piles share neither height nor color, so no move is legal.

Example 3

heights = [1,1,1]topColors = ["R","G","B"]return = true

Merge any two equal-height piles. The remaining piles then have different heights and colors, so the opponent has no move.

Constraints

  • 1 <= heights.length = topColors.length <= 12.
  • 1 <= heights[i] <= 12.
  • Each color is a non-empty uppercase string.
  • The sum of all heights is at most 12.

More Google problems

See Google hiring insights
public boolean canCurrentPlayerWin(int[] heights, String[] topColors) {
    // Write your code here.
}
heights[1,1]
topColors["R","B"]
expectedtrue
Checking account…