-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy path279. Perfect Squares.c
55 lines (37 loc) · 1.13 KB
/
279. Perfect Squares.c
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
52
53
54
55
/*
279. Perfect Squares
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n.
For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 13, return 2 because 13 = 4 + 9.
Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
*/
int numSquares(int n) {
int i, j, k;
int *dp;
dp = malloc((n + 1) * sizeof(int));
//assert(dp);
dp[0] = 0;
for (i = 1; i <= n; i ++) {
k = dp[i - 1] + 1; // answer of preceeding number plus 1
j = 2;
while (i >= j * j) { // after j ^ 2
if (k > dp[i - j * j] + 1) {
k = dp[i - j * j] + 1;
}
j ++;
}
dp[i] = k;
}
k = dp[n];
free(dp);
return k;
}
/*
Difficulty:Medium
Total Accepted:81.3K
Total Submissions:220.9K
Companies Google
Related Topics Dynamic Programming Breadth-first Search Math
Similar Questions
Count Primes
Ugly Number II
*/