Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Move from brute-force thinking to an efficient approach using math strategy.
Given an integer n, return the least number of perfect square numbers that sum to n.
A perfect square is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, 1, 4, 9, and 16 are perfect squares while 3 and 11 are not.
Example 1:
Input: n = 12 Output: 3 Explanation: 12 = 4 + 4 + 4.
Example 2:
Input: n = 13 Output: 2 Explanation: 13 = 4 + 9.
Constraints:
1 <= n <= 104Problem summary: Given an integer n, return the least number of perfect square numbers that sum to n. A perfect square is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, 1, 4, 9, and 16 are perfect squares while 3 and 11 are not.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math · Dynamic Programming
12
13
count-primes)ugly-number-ii)ways-to-express-an-integer-as-sum-of-powers)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #279: Perfect Squares
class Solution {
public int numSquares(int n) {
int m = (int) Math.sqrt(n);
int[][] f = new int[m + 1][n + 1];
for (var g : f) {
Arrays.fill(g, 1 << 30);
}
f[0][0] = 0;
for (int i = 1; i <= m; ++i) {
for (int j = 0; j <= n; ++j) {
f[i][j] = f[i - 1][j];
if (j >= i * i) {
f[i][j] = Math.min(f[i][j], f[i][j - i * i] + 1);
}
}
}
return f[m][n];
}
}
// Accepted solution for LeetCode #279: Perfect Squares
func numSquares(n int) int {
m := int(math.Sqrt(float64(n)))
f := make([][]int, m+1)
const inf = 1 << 30
for i := range f {
f[i] = make([]int, n+1)
for j := range f[i] {
f[i][j] = inf
}
}
f[0][0] = 0
for i := 1; i <= m; i++ {
for j := 0; j <= n; j++ {
f[i][j] = f[i-1][j]
if j >= i*i {
f[i][j] = min(f[i][j], f[i][j-i*i]+1)
}
}
}
return f[m][n]
}
# Accepted solution for LeetCode #279: Perfect Squares
class Solution:
def numSquares(self, n: int) -> int:
m = int(sqrt(n))
f = [[inf] * (n + 1) for _ in range(m + 1)]
f[0][0] = 0
for i in range(1, m + 1):
for j in range(n + 1):
f[i][j] = f[i - 1][j]
if j >= i * i:
f[i][j] = min(f[i][j], f[i][j - i * i] + 1)
return f[m][n]
// Accepted solution for LeetCode #279: Perfect Squares
impl Solution {
pub fn num_squares(n: i32) -> i32 {
let (row, col) = ((n as f32).sqrt().floor() as usize, n as usize);
let mut dp = vec![vec![i32::MAX; col + 1]; row + 1];
dp[0][0] = 0;
for i in 1..=row {
for j in 0..=col {
dp[i][j] = dp[i - 1][j];
if j >= i * i {
dp[i][j] = std::cmp::min(dp[i][j], dp[i][j - i * i] + 1);
}
}
}
dp[row][col]
}
}
// Accepted solution for LeetCode #279: Perfect Squares
function numSquares(n: number): number {
const m = Math.floor(Math.sqrt(n));
const f: number[][] = Array(m + 1)
.fill(0)
.map(() => Array(n + 1).fill(1 << 30));
f[0][0] = 0;
for (let i = 1; i <= m; ++i) {
for (let j = 0; j <= n; ++j) {
f[i][j] = f[i - 1][j];
if (j >= i * i) {
f[i][j] = Math.min(f[i][j], f[i][j - i * i] + 1);
}
}
}
return f[m][n];
}
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.