LeetCode 3360: Stone Removal Game Solution

Master LeetCode problem 3360 (Stone Removal Game), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

3360. Stone Removal Game

Problem Explanation

Explanation:

To solve this problem, we can simulate the game between Alice and Bob iteratively. We start with Alice removing 10 stones and then alternate turns between the two players, removing stones according to the rules until one player cannot make a move. We keep track of whose turn it is and the number of stones remaining. If the number of stones is less than 10 and it's Alice's turn, she loses.

Algorithm:

  1. Initialize a variable stones to n and a boolean variable aliceTurn to true.
  2. Iterate while stones is greater than 0:
    • If aliceTurn is true, Alice removes stones as per the rules.
    • If aliceTurn is false, Bob removes stones as per the rules.
    • Decrement the number of stones removed from stones and toggle aliceTurn.
  3. If the loop ends with aliceTurn as true, return true (Alice wins); otherwise, return false (Alice loses).

Time Complexity:

The time complexity of this approach is O(n), where n is the input integer.

Space Complexity:

The space complexity is O(1) as we are using only a fixed amount of extra space.

: :

Solution Code

class Solution {
    public boolean stoneGame(int n) {
        int stones = n;
        boolean aliceTurn = true;

        while (stones > 0) {
            if (aliceTurn) {
                stones -= 10;
            } else {
                stones -= 11 - stones % 11;
            }
            aliceTurn = !aliceTurn;
        }

        return aliceTurn;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 3360 (Stone Removal Game)?

This page provides optimized solutions for LeetCode problem 3360 (Stone Removal Game) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 3360 (Stone Removal Game)?

The time complexity for LeetCode 3360 (Stone Removal Game) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 3360 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 3360 in Java, C++, or Python.

Back to LeetCode Solutions