Skip to content

3360. Stone Removal Game 👍

  • Time: $O(10) = O(1)$
  • Space: $O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
 public:
  bool canAliceWin(int n) {
    for (int stones = 10; stones >= 0; --stones) {
      if (stones > n)
        return stones % 2 == 1;
      n -= stones;
    }
    throw;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution {
  public boolean canAliceWin(int n) {
    for (int stones = 10; stones >= 0; --stones) {
      if (stones > n)
        return stones % 2 == 1;
      n -= stones;
    }
    throw new IllegalArgumentException();
  }
}
1
2
3
4
5
6
class Solution:
  def canAliceWin(self, n: int) -> bool:
    for stones in range(10, -1, -1):
      if stones > n:
        return stones % 2 == 1
      n -= stones