3360 - Stone Removal Game (Easy)
Problem Link
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 because Alice must remove exactly 10 stones on her first turn. After every successful move, we subtract from and decrease by . This represents the required sequence of moves: .
The loop continues as long as there are at least stones available. When , the current player cannot make the required move, so that player loses. Alice takes turns when is even (), while Bob takes turns when is odd (). Therefore, if the loop ends with an odd , it is Bob's turn and he cannot move, meaning Alice wins.
The time complexity is and the space complexity is .
class Solution:
def canAliceWin(self, n: int) -> bool:
x = 10
while n >= x:
n -= x
x -= 1
return x % 2 == 13331 - Find Subtree Sizes After Changes
Find subtree sizes after tree reparenting using iterative DFS, nearest matching ancestor tracking, and bottom-up aggregation in O(n) time.
3364 - Minimum Positive Sum Subarray
Find the minimum positive sum of a subarray with a length between l and r using a simple running-sum approach.