2Brute2Best
🔥0

Perfect Squares

Medium·LeetCode #279 Pro OnlyPattern Transfer
LeetCode
01

Problem Statement

You are given a whole number n. A perfect square is a number like 1, 4, 9, 16 and so on, which is a whole number multiplied by itself.

Find the fewest perfect squares that add up to exactly n. A square can be used more than once.

Return that smallest count.

Input

A whole number n.

Output

The fewest perfect squares whose sum is n.

Example 1
Input: n = 12
Output: 3

12 = 4 + 4 + 4. No two squares make 12 (for example 9 + 3 and 4 + 8 do not work), so 3 is the least.

Example 2
Input: n = 13
Output: 2

13 = 4 + 9. 13 is not a square itself, so one square is not enough.

Constraints
  • • 1 ≤ n ≤ 10^4
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“the fewest perfect squares”
→ A minimum count, so dp holds numbers and combines them with min. Word Break combined yes-or-no answers with OR.
“add up to exactly `n`”
→ The pieces must sum to n. Track the amount left, not a position in a string.
“a square can be used more than once”
→ Reuse is allowed, so dp[i − k·k] is read as it is, with no "used" flags.
03

Approach Complexity Ladder

Brute Force — try every square, then solve the restO(√N · 2^N)O(N)

For an amount left, try every square that fits, then solve the smaller amount

Optimal — bottom-up table over 0 … nO(N√N)O(N)

dp[i] = 1 + the best dp[i − k·k] over every square ≤ i; fill from 0 up to n

Brute Force Intuition

For an amount left, ask how few squares make it. Try every square k·k that is at most left. Take it, and ask the same question for left − k·k. Add one for the square taken, and keep the smallest total. When left is 0, no squares are needed.

Recognition rule for this sub-pattern:

dp[i] = can the first i characters be split validly? Try every last piece.
Remember this · DP — 1D Linear
Trigger
"Number of ways", "maximum/minimum up to i", choices at each step that depend on the previous one or two.
Think
If I knew the answer for every smaller prefix, how would I get the answer for i?
Key question
What does dp[i] mean in one sentence?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like DP — 1D Linear translate into production architectures and low-level memory mechanics.