Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4412370

Browse filesBrowse files
authored
Create 14_pefectSquares.cpp
1 parent b89152b commit 4412370
Copy full SHA for 4412370

File tree

Expand file treeCollapse file tree

1 file changed

+18
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+18
-0
lines changed
+18Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.