dp
of size n+1
with all elements set to n+1
.dp[0]
to 0
as the base case.1
to n
(inclusive) to fill the DP array:
a. For each i
from 1
to n
, loop through all square numbers j*j
where j*j <= i
.
b. Update dp[i]
to be the minimum of dp[i]
and dp[i - j*j] + 1
.dp[n]
as the result.