Back to August Back to Year 2026
HARD 10 Aug 2026 View on LeetCode

1510. Stone Game IV

</> Solution

/**
 * Problem: LeetCode 1510 - Stone Game IV
 * Difficulty: Hard
 * Language: Java
 * Author: CodeByTushu
 */
class Solution {
    public boolean winnerSquareGame(int n) {
        boolean[] dp = new boolean[n + 1];
        for (int i = 1; i <= n; i++) {
            for (int k = 1; k * k <= i; k++) {
                if (!dp[i - k * k]) {
                    dp[i] = true;
                    break;
                }
            }
        }
        return dp[n];
    }
}

TIME COMPLEXITY

O(N sqrt(N))

SPACE COMPLEXITY

O(N)

TOPICS

Dynamic Programming Game Theory Math