FunctionRacerProblemsRace

Take One, Two or Three

BeginnerDynamic programmingPython, JavaScript, TypeScript, Java, C++

Two players share a pile of stones. They take turns, and on a turn a player removes 1, 2 or 3 stones. Whoever takes the last stone wins.

Both play as well as possible. Return True if the player who moves first wins, and False otherwise.

Example 1

stones 4
returns false

Whatever the first player takes, 1, 2 or 3, the second player takes the rest.

Example 2

stones 5
returns true

The first player takes 1 and leaves 4, which is a losing pile for the one to move.

Example 3

stones 3
returns true

The first player takes all three.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Dynamic programming problems