-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode279.cpp
45 lines (41 loc) · 1.05 KB
/
leetcode279.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
/*************************************************
Author: wenhaofang
Date: 2023-03-21
Description: leetcode279 - Perfect Squares
*************************************************/
#include <bits/stdc++.h>
using namespace std;
/**
* 方法一:动态规划
*
* 理论时间复杂度:O(n * sqrt(n)),其中 n 为数字大小
* 理论空间复杂度:O(n) ,其中 n 为数字大小
*/
class Solution {
public:
int numSquares(int n) {
vector<int> nums;
for (int i = 1; i <= sqrt(n); i++) {
nums.push_back(i * i);
}
vector<int> dp(n + 1, INT_MAX);
dp[0] = 0;
for (int i = 1; i <= n; i++) {
for (int num: nums) {
if (num <= i) {
dp[i] = min(dp[i], dp[i - num] + 1);
}
}
}
return dp[n];
}
};
/**
* 测试
*/
int main() {
Solution* solution = new Solution();
int n = 12;
int ans = solution -> numSquares(n);
cout << ans << endl;
}