-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0279.cpp
51 lines (46 loc) · 933 Bytes
/
0279.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <vector>
using namespace std;
class Solution
{
public:
/*
int numSquares(int n)
{
vector<int> dp(n + 1, INT_MAX);
dp[0] = 0;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j * j <= i; j++)
{
dp[i] = min(dp[i], dp[i - j * j] + 1);
}
}
return dp[n];
}
*/
int numSquares(int n)
{
int m = sqrt(n);
vector<int> dp(n + 1, INT_MAX - 1);
dp[0] = 0;
for (int i = 1; i <= m; i++)
{
for (int j = 1; j <= n; j++)
{
if (j >= pow(i, 2))
dp[j] = min(dp[j], dp[j - pow(i, 2)] + 1);
//cout << dp[j] << " ";
}
//cout << endl;
}
return dp[n];
}
};
int main()
{
int n = 12;
Solution S;
S.numSquares(n);
return 0;
}