Tony's Log

Algorithms, Distributed System, Machine Learning

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

An intuitive DP.

class Solution {
public:
    int numSquares(int n) {
        vector<int> dp(n + 1, INT_MAX);
        dp[0] = 0;
        
        for(int i = 0; i <= n; i ++)
            for(int j = 1; j * j <= n - i; j ++)
                dp[i + j * j] = min(dp[i + j * j], dp[i] + 1);
        
        return dp[n];
    }
};
posted on 2015-09-12 08:43  Tonix  阅读(140)  评论(0编辑  收藏  举报