forked from yubinbai/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
45 lines (42 loc) · 854 Bytes
/
main.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
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
class Solution {
private:
vector<int> memo;
int _numSquares(int n) {
if (memo[n] != -1) {
return memo[n];
}
int result = INT_MAX;
for (int i = 1; i * i <= n; i++) {
result = min(result, 1 + _numSquares(n - i * i));
}
return memo[n] = result;
}
public:
int numSquares(int n) {
if (n > 0) {
memo.resize(n + 1, -1);
memo[0] = 0;
return _numSquares(n);
} else {
return -1;
}
}
};
int main() {
Solution sol;
cout << sol.numSquares(6) << endl;
cout << sol.numSquares(12) << endl;
cout << sol.numSquares(13) << endl;
cout << sol.numSquares(9732) << endl;
return 0;
}