3360. Stone Removal Game

LeetCode 力扣

1. Description

Alice and Bob alternate taking stones from a pile of n stones, with Alice moving first. Her first move must remove exactly 10 stones.
Every later move must remove exactly one fewer stone than the previous move. A player who cannot remove the required number loses.
Return true if Alice wins, or false if Bob wins.

2. Example

Example 1

Input: n = 12
Output: true
Explanation: Alice takes 10 stones, leaving 2. Bob needs to take 9 stones but cannot, so Alice wins.

Example 2

Input: n = 1
Output: false
Explanation: Alice cannot take the required 10 stones on her first turn, so she loses.

3. Constraints

  • 1 <= n <= 50

4. Solutions

Simulation

Time complexity: O(1)
Space complexity: O(1)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
public:
    bool canAliceWin(int n) {
        int count = 10;
        bool alice_win = false;
        while (n >= count) {
            n -= count;
            --count;
            alice_win = !alice_win;
        }

        return alice_win;
    }
};
comments powered by Disqus