FastPrepStone Game III

Stone Game III

Google logoGoogle● HardFULLTIMEPHONE SCREEN
Learn

Problem statement

Alice and Bob play a game using the integer array stoneValue. Alice moves first. On each turn, a player removes the first 1, 2, or 3 remaining stones and adds their values to that player's score. Play continues until no stones remain.

Both players choose moves that maximize their own final score. Return "Alice" if Alice's score is greater, "Bob" if Bob's score is greater, or "Tie" if the scores are equal.

Function

stoneGameIII(stoneValue: int[]) → String

Examples

Example 1

stoneValue = [1,2,3,7]return = "Bob"

No matter whether Alice takes one, two, or three stones, Bob can take the remaining high-value group and finish with the larger score.

Example 2

stoneValue = [1,2,3,-9]return = "Alice"

Alice can take the first three stones for 6, leaving -9 for Bob.

Example 3

stoneValue = [1,2,3,6]return = "Tie"

With optimal play, Alice takes the first three stones for 6 and Bob takes the final stone worth 6.

Constraints

  • 1 <= stoneValue.length <= 50000.
  • -1000 <= stoneValue[i] <= 1000.

More Google problems

See Google hiring insights
public String stoneGameIII(int[] stoneValue) {
    // Write your code here.
}
stoneValue[1,2,3,7]
expected"Bob"
Checking account…