279. Perfect Squares Add to List QuestionEditorial Solution

典型的背包问题,假设小于n的完全平方数为Si,那么dp[n]=min(dp[n-S1],dp[n-S2],…dp[n-Si])+1

class Solution {
public:
	int numSquares(int n) {
		vector<int>dp(n + 1, INT_MAX);
		dp[0]=0;
		dp[1] = 1;
		for (int i = 2; i < n + 1; ++i)
		{
			for (int j = 1; j*j <= i;++j)
			dp[i] = min(dp[i],dp[i - j*j] + 1);
		}
		return dp[n];
	}
};


posted @ 2016-12-17 09:17  Initial_Dream  阅读(179)  评论(0)    收藏  举报