Skip to content

Commit

Permalink
add 0279_perfect_squares
Browse files Browse the repository at this point in the history
  • Loading branch information
newyork-anthonyng committed Aug 28, 2020
1 parent a51db4b commit 3e22f9a
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions leetcode/0279_perfect_squares.js
@@ -0,0 +1,20 @@
function numSquares(n) {
const dp = [];
dp[0] = 0;
dp[1] = 1;

for (let i = 2; i <= n; i++) {
dp[i] = Infinity;

for (let j = 1; (j * j) <= i; j++) {
dp[i] = Math.min(dp[i], dp[i - (j * j)] + 1);
}
}

return dp[n];
}

console.assert(
numSquares(12) === 3,
'first'
);

0 comments on commit 3e22f9a

Please sign in to comment.