Skip to content

Commit

Permalink
Time: 66 ms (91.68%), Space: 6 MB (92.63%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
nilumbra committed Feb 13, 2023
1 parent ab41680 commit 8557e3f
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions 0279-perfect-squares/0279-perfect-squares.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public:
int numSquares(int n) {
int arr[n+1];
arr[0] = 0;
for (int i = 1; i <= n; i++) {
int minSteps = 100000;
// for each i, find all the steps available
for (int sqrt = 1; sqrt * sqrt <= i; sqrt++) {
//.minimum steps need to get to (i - sqrt * sqrt)
if (arr[i - sqrt * sqrt] + 1 < minSteps)
minSteps = arr[i - sqrt * sqrt] + 1;
}
arr[i] = minSteps;
}

return arr[n];
}
};

0 comments on commit 8557e3f

Please sign in to comment.