-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Time: 66 ms (91.68%), Space: 6 MB (92.63%) - LeetHub
- Loading branch information
Showing
1 changed file
with
19 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
}; |