1449. Restricted Take Game
Several jars of marbles stand on a table; jar i holds piles[i] marbles. The array moves lists the allowed take sizes (repeats are harmless). On a turn the current player picks one jar and removes exactly s marbles from it, where s is some value in moves and the jar must contain at least s marbles. The first player moves first, players alternate, and a player with no legal move loses. Both play perfectly.
Return true if the first player wins. With M the largest pile and m = moves.length, aim for O(M * m + n) time and O(M) space.
Example 1
- Input:
- piles = [5], moves = [1,2]
- Output:
- true
- Explanation:
- Single pile of 5 with takes 1 or 2: leaving a multiple of 3 is always possible, so the first player wins by taking 2.
Example 2
- Input:
- piles = [3], moves = [1,2]
- Output:
- false
- Explanation:
- A pile of 3 is a losing position because any take leaves 1 or 2 for the opponent.
Example 3
- Input:
- piles = [5,6], moves = [2,3]
- Output:
- false
- Explanation:
- With takes of 2 or 3, both piles of 5 and 6 are losing for the player to move, so the first player loses.
Constraints
1 ≤ piles.length ≤ 105
0 ≤ piles[i] ≤ 104
1 ≤ moves.length ≤ 20; 1 ≤ moves[j] ≤ 104 (repeats allowed)
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 1,200 msC++ 300 msJava 600 msJavaScript 600 msTypeScript 600 ms
Expected complexity
- Time
- O(M * m + n)
- Space
- O(M)