Skip to content

Commit 4412370

Browse files
authored
Create 14_pefectSquares.cpp
1 parent b89152b commit 4412370

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 numSquares(int n) {
4+
//vector for updating the dp array/values
5+
vector<int> dp(n+1,INT_MAX);
6+
//base case
7+
dp[0]=0;
8+
int count = 1;
9+
while(count*count <= n) {
10+
int sq = count*count;
11+
for(int i = sq; i < n+1; i++) {
12+
dp[i] = min(dp[i-sq] + 1,dp[i]);
13+
}
14+
count++;
15+
}
16+
return dp[n];
17+
}
18+
};

0 commit comments

Comments
 (0)