Skip to Content
LeetCode The Hard Way
3300 - 3399

3360 - Stone Removal Game (Easy)

https://leetcode.com/problems/stone-removal-game

Problem Statement

Alice and Bob are playing a game where they take turns removing stones from a pile, with Alice going first.

  • Alice starts by removing exactly 10 stones on her first turn.
  • For each subsequent turn, each player removes exactly 1 fewerstonethan the previous opponent.

The player who cannot make a move loses the game.

Given a positive integer n, return true if Alice wins the game and false otherwise.

Example 1:

Input: n = 12

Output: true

Explanation:

  • Alice removes 10 stones on her first turn, leaving 2 stones for Bob.
  • Bob cannot remove 9 stones, so Alice wins.

Example 2:

Input: n = 1

Output: false

Explanation:

  • Alice cannot remove 10 stones, so Alice loses.

Constraints:

  • 1 <= n <= 50

Approach 1: Simulation

We start with x=10x = 10 because Alice must remove exactly 10 stones on her first turn. After every successful move, we subtract xx from nn and decrease xx by 11. This represents the required sequence of moves: 10,9,8,...10, 9, 8, ....

The loop continues as long as there are at least xx stones available. When n<xn < x, the current player cannot make the required move, so that player loses. Alice takes turns when xx is even (10,8,6,...10, 8, 6, ...), while Bob takes turns when xx is odd (9,7,5,...9, 7, 5, ...). Therefore, if the loop ends with an odd xx, it is Bob's turn and he cannot move, meaning Alice wins.

The time complexity is O(1)O(1) and the space complexity is O(1)O(1).

Written by@wkw
class Solution:
    def canAliceWin(self, n: int) -> bool:
        x = 10
        while n >= x:
            n -= x
            x -= 1
        return x % 2 == 1

On this page