279 Perfect Squares
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n.
Example 1:
Input: n = 12
Output: 3
Explanation: 12 = 4 + 4 + 4.
Example 2:
Input: n = 13
Output: 2
Explanation: 13 = 4 + 9.
@tag-dp

Algorithm

Create a one-dimensional dp array of length n+1, initialize the first value to 0, and initialize the remaining values to INT_MAX

i loops from 0 to n, j loops from 1 to the position of i+jj <= n, and then updates the value of dp[i+jj] each time, dynamically updating the dp array, where dp[i] means positive The integer i can be composed of multiple perfect squares.

Note that the wording here, i must start from 0, j must start from 1. Because our original intention is to use dp[i] to update dp[i + j * j], if i=0, j=1 , Then dp[i] and dp[i + j * j] are equal.