leetcode@ [279]Perfect Squares

https://leetcode.com/problems/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.

class Solution {
public:
int getMaximumSquare(int n){
int ret = (int)sqrt(n);
return ret;
}
int dfs(int load, vector<int> &dp){
if(dp[load]) return dp[load];
if(load == ){
return ;
} int next = getMaximumSquare(load);
int Min = numeric_limits<int>::max();
for(int v=next;v>=;v--){
if(load-v*v >= ){
Min = min(Min, dfs(load-v*v, dp));
}
}
dp[load] = Min + ;
return dp[load];
}
int numSquares(int n) {
vector<int> dp(n+);
for(int i=;i<dp.size();++i) dp[i] = ; dp[n] = dfs(n, dp);
return dp[n];
}
};
 
上一篇:BGP:我们不生产路由,而是路由的搬运工


下一篇:Vue组件选项props