Skip to content

Commit e4b06f6

Browse files
committed
Time: 584 ms (22.73%), Space: 9.9 MB (26.62%) - LeetHub
1 parent d25c667 commit e4b06f6

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int helper(int i,vector<int>&dp){
4+
if(i==0) return 0;
5+
if(dp[i]!=-1) return dp[i];
6+
int minCnt=INT_MAX;
7+
for(int num=1;num<=sqrt(i);++num){
8+
int curCnt=1+helper(i-(num*num),dp);
9+
minCnt=min(curCnt,minCnt);
10+
}
11+
return dp[i]=minCnt;
12+
}
13+
14+
int numSquares(int n) {
15+
vector<int>dp(n+1,-1);
16+
return helper(n,dp);
17+
}
18+
};

0 commit comments

Comments
 (0)