(1)Best way.94%

This one uses Lagrange's four-square theorem.

According to it, every integer is a sum of at most 4 perfect square numbers. Therefore the result should be one of 1,2,3,4.

class Solution {
  public:
  int numSquares(int n) {
    while (n % 4 == 0) n /= 4;//no effect on result, but simplified calculate.
    if (n % 8 == 7) return 4;//by theorem.
    for (int a = 0; a * a <= n; ++a) {
    int b = sqrt(n - a * a);
    if (a * a + b * b == n)

    {
    return !!a + !!b;//!!is to find out if a,b is 0 or 1.
    }
  }
    return 3;
}
};

(2)DP:

Actually the 2 ways below use the same method. The only difference is they use different containers. One uses hmap, another one uses vector. But the result of time has a great difference.I do not know why, and hope someone can answer.

class Solution {
  public:
    int numSquares(int n) {
      unordered_map<int,int> hmap;
      if(n==1)
      return 1;
      hmap[0]=0;
      for(int m=1;m<=n;m++)
      {
        int minval=INT_MAX;
        for(int j=1;j*j<=m;j++)
        {
        hmap[j*j]=1;
        minval=min(minval,hmap[j*j]+hmap[m-j*j]);
        }
       hmap[m]=minval;
      }
       return hmap[n];
  }
};

This one is not good. Time Limit Exceeded, time 5673ms.

Let's see another one.

class Solution {
  public:
    int numSquares(int n) {
    vector<int> hmap(1,0);
    for(int m=1;m<=n;m++)
    {
      int minval=INT_MAX;
      for(int j=1;j*j<=m;j++)
      {
      minval=min(minval,1+hmap[m-j*j]);
      }
      hmap.push_back(minval);
    }
      return hmap.back();
  }
};

This one is better. 46% and runing time is 320ms.

转载于:https://www.cnblogs.com/CathyXiaohe/p/4985374.html

Xiaohe-LeetCode 279 Perfect Squares相关推荐

  1. LeetCode 279. Perfect Squares

    279. Perfect Squares Given a positive integer n, find the least number of perfect square numbers (fo ...

  2. leetcode -- 279. Perfect Squares

    Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...

  3. leetcode 279. Perfect Squares | 279. 完全平方数(动态规划,Java)

    题目 https://leetcode.com/problems/perfect-squares/ 题解:动态规划 参考:[宫水三叶]详解完全背包一维空间优化推导(附背包问题攻略) 首先初始化长度为 ...

  4. Leetcode 279. Perfect Square

    Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...

  5. 279 Perfect Squares 完美平方数

    给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...) 使得他们的和等于 n.你需要让平方数的个数最少. 比如 n = 12,返回 3 ,因为 12 = 4 + 4 + 4 : ...

  6. 279. Perfect Squares

    Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...

  7. 【Breadth-first Search 】279. Perfect Squares

    输入:一个非负整数n. 输出:这个非负整数可以写成几个完全平方数.返回这个数量. 规则:完全平方数可以表示为某个整数的平方.例如:1,4,9- 分析:13=4+9 也就是说13可以写成2个完全平方数的 ...

  8. [LeetCode]Perfect Squares

    题目链接:Perfect Squares 题目内容: Given a positive integer n, find the least number of perfect square numbe ...

  9. 4kyu Sums of Perfect Squares

    4kyu Sums of Perfect Squares 题目背景: The task is simply stated. Given an integer n (3 < n < 109) ...

最新文章

  1. 怎么成为优秀的软件模型设计者?
  2. 成为算法工程师的路上,掌握什么思维会让自我提升突飞猛进?
  3. Genome Biology:赵方庆组揭示生命早期肠道菌群演变规律及决定因素
  4. 内存都是由半导体器件构成的_开启5G新时代——XPS成像技术在半导体器件中的应用...
  5. java 实现 web 客户端_Java web客户端和服务器端交互的原理
  6. python条形图颜色设置_python – 根据值在matplotlib中更改3D条形图中的条形颜色
  7. 基于单片机步进电机ppt答辩_基于单片机的步进电机式汽车仪表的设计(含电路原理图,程序)...
  8. Win32ASM-进程学习[3]-读写进程空间
  9. arcgis字段计算器--随机数
  10. 这么做科研你也能成功!
  11. 《软件工程概论》第四章核心内容
  12. Deskreen – 将电脑屏幕共享到浏览器中,做第二块屏幕[Win/macOS/Linux]
  13. 《基于MFC的OpenGL编程》Part 14 Quadrics
  14. 所谓伊人,在水一方--------------折射定律
  15. selenium常用的API
  16. 2010中国存储创新年会
  17. win10 系统 程序员计算器面板介绍和功能使用
  18. 计算机驱动程序的安装过程,电脑怎么安装驱动程序
  19. 计算机硬件的组装硬盘,组装电脑如何选择硬盘?DIY装机四种电脑硬盘搭配方案提供参考...
  20. PCA9685--16路 PWM模块舵机驱动板--STM32 IIC接口模块

热门文章

  1. odoo 中英文翻译设置
  2. 三星e210l android5.0,[合作版]MIUI V5_三星S3 E210L_4.8.8
  3. 别只用 Selenium,新神器 Pyppeteer 绕过淘宝更简单!
  4. 南阳计算机职称考试报名时间2015,南阳市2015年上半年全国计算机等级考试报名人数共7718人...
  5. 远程和Ubuntu服务器进行Socket通信,使用python和C#(准备篇)
  6. Codeforces Round #736 (Div. 2)_C. Web of Lies
  7. IM即时通讯H5端,MQTT
  8. python print 中文显示\xe3\x80
  9. 无线路由器信道怎么设置 无线路由器信道选择哪个好
  10. Azure DevOps Server 2022新功能:禁止用户管理自己创建的分支(mange-permission)