-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_0279_numSquares.cc
122 lines (114 loc) · 2.3 KB
/
Problem_0279_numSquares.cc
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <cstdint>
#include <iostream>
#include <vector>
#include "UnitTest.h"
using namespace std;
class Solution
{
public:
// 暴力解
int numSquares1(int n)
{
int ans = n;
int num = 2;
while (num * num <= n)
{
int a = n / (num * num);
int b = n % (num * num);
ans = std::min(ans, a + numSquares1(b));
num++;
}
return ans;
}
// 1 : 1, 4, 9, 16, 25, 36, ...
// 4 : 7, 15, 23, 28, 31, 39, 47, 55, 60, 63, 71, ...
// 规律解
// 规律一:个数不超过4
// 规律二:出现1个的时候,显而易见
// 规律三:任何数 % 8 == 7,一定是4个
// 规律四:任何数消去4的因子之后,剩下rest,rest % 8 == 7,一定是4个
int numSquares2(int n)
{
// TODO: figure it out
int rest = n;
while (rest % 4 == 0)
{
rest /= 4;
}
if (rest % 8 == 7)
{
return 4;
}
int f = (int) sqrt(n);
if (f * f == n)
{
return 1;
}
for (int first = 1; first * first <= n; first++)
{
int second = (int) sqrt(n - first * first);
if (first * first + second * second == n)
{
return 2;
}
}
return 3;
}
// 数学解
// 1)四平方和定理
// 2)任何数消掉4的因子,结论不变
int numSquares3(int n)
{
// TODO: figure it out
while (n % 4 == 0)
{
n /= 4;
}
if (n % 8 == 7)
{
return 4;
}
for (int a = 0; a * a <= n; ++a)
{
int b = (int) sqrt(n - a * a);
if (a * a + b * b == n)
{
return (a > 0 && b > 0) ? 2 : 1;
}
}
return 3;
}
// 暴力解改动态规划
int numSquares4(int n)
{
vector<int> dp(n + 1);
for (int i = 1; i <= n; i++)
{
int num = INT32_MAX;
for (int j = 1; j * j <= i; j++)
{
num = std::min(num, dp[i - j * j]);
}
dp[i] = num + 1;
}
return dp[n];
}
};
void testNumSquares()
{
Solution s;
EXPECT_EQ_INT(3, s.numSquares1(12));
EXPECT_EQ_INT(2, s.numSquares1(13));
EXPECT_EQ_INT(3, s.numSquares2(12));
EXPECT_EQ_INT(2, s.numSquares2(13));
EXPECT_EQ_INT(3, s.numSquares3(12));
EXPECT_EQ_INT(2, s.numSquares3(13));
EXPECT_EQ_INT(3, s.numSquares4(12));
EXPECT_EQ_INT(2, s.numSquares4(13));
EXPECT_SUMMARY;
}
int main()
{
testNumSquares();
return 0;
}