LeetCode 279. 完全平方数

给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, …)使得它们的和等于 n。你需要让组成和的完全平方数的个数最少。

示例 1:

输入: n = 12
输出: 3
解释: 12 = 4 + 4 + 4.
示例 2:

输入: n = 13
输出: 2
解释: 13 = 4 + 9.

法1: 动态规划,时间复杂度O(n*sqrt(n)), 空间复杂度O(n)

class Solution {
public:
    int numSquares(int n) {
        vector<int> dp(n+1, 0);
        vector<int> mults;
        int i = 1;
        while(i*i <= n) {
            mults.push_back(i*i);
            i++;
        }
        for (int i = 1; i <= n; i++)
        {
            int min_nums = i+1;
            for (int j = 0; j < mults.size() && i >= mults[j]; j++)
            {
                min_nums = min(min_nums, dp[i - mults[j]] + 1 );
            }
            dp[i] = min_nums;
        }
        return dp[n];
    }
};

法2:数学方法, 时间复杂度:O(sqrt(n)), 空间复杂度O(1).

class Solution {

  protected boolean isSquare(int n) {
    int sq = (int) Math.sqrt(n);
    return n == sq * sq;
  }

  public int numSquares(int n) {
    // four-square and three-square theorems.
    while (n % 4 == 0)
      n /= 4;
    if (n % 8 == 7)
      return 4;

    if (this.isSquare(n))
      return 1;
    // enumeration to check if the number can be decomposed into sum of two squares.
    for (int i = 1; i * i <= n; ++i) {
      if (this.isSquare(n - i * i))
        return 2;
    }
    // bottom case of three-square theorem.
    return 3;
  }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值