Skip to content

Commit 86d498b

Browse files
committed
Solved 279
1 parent cb11238 commit 86d498b

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

200-299/279.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* DP Solution 168ms
3+
*
4+
* @param {number} n
5+
* @return {number}
6+
*/
7+
var numSquares = function(n) {
8+
if (n <= 0) return 0;
9+
let memo = [0];
10+
11+
while (memo.length <= n) {
12+
console.log(memo);
13+
let m = memo.length;
14+
let cntSquares = Number.MAX_SAFE_INTEGER;
15+
for (let i = 1; i * i <= m; i++) {
16+
cntSquares = Math.min(cntSquares, memo[m - i * i] + 1);
17+
}
18+
19+
memo.push(cntSquares);
20+
}
21+
22+
return memo[n];
23+
};
24+
25+
const input = 12;
26+
27+
console.log(numSquares(input));

0 commit comments

Comments
 (0)