Precompute perfect squares up to : . For each state, iterate through valid squares. Early termination: if any move leads to a losing state, we win. Stop as soon as we find one. Memoization: use array . Fill bottom-up since is always smaller than . Space: . Time: worst case, but early termination helps in practice. Precompute all perfect squares up to n. This avoids repeated square root computations in the inner loop.
Time complexity: .
Space complexity: .