leetcode 279. Perfect Squares(完美平方数)

LeetCode刷题:279. Perfect Squares 完美平方数的DP解法 LeetCode刷题:279. Perfect Squares 完美平方数的DP解法 原题链接: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, ... 阅读详情

Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, …) which sum to n.

Example 1:

Input: n = 12
Output: 3
Explanation: 12 = 4 + 4 + 4.
Example 2:

Input: n = 13
Output: 2
Explanation: 13 = 4 + 9.

给出一个数字,问该数字最少由几个平方数的和构成
比如13, 最少由42和92这两个平方数构成,所以返回2

思路:
方法一:DP
首先知道自己就是平方的数返回1,因为它们由它们自己构成

定义dp数组,dp[i] = i 由几个平方数构成
比如dp[12] = 3, dp[13] = 2

已知dp[i * i] = 1,那么dp[i * i + 1] = dp[i * i] + 12 或者dp[i * i + 1]中较小的(有可能 dp[i * i + 1]前面已经计算过)
而后再算dp[i * i + 2] …一直计算到i * i + j <= n
然后i 向前走,从下一个平方数的dp[i * i] =1开始
该方法简化了一些运算量,但是不好理解

 //17ms
    public int numSquares(int n) {
        if (n == 0) {
            return 0;
        }
        
        int[] dp = new int[n+1];
        Arrays.fill(dp, Integer.MAX_VALUE);
        dp[0] = 0;
        
        //square itself is 1..
        for (int i = 1; i * i <= n; i++) {
            dp[i * i] = 1;
        }
        
        for (int i = 1; i * i <= n; i++) {
            int k = i * i;
            //j从k开始是因为直接从2*i^2处简化,dp[2*i^2]=dp[i^2]+1=2,
            //从这里开始往后走可以避免一部分计算量
            //相当于划成了每个i*i的小区间,每次从i*i开始更新
            for (int j = k; j + k <= n; j++){
                dp[j + k] = Math.min(dp[j + k], dp[j] + 1);
            }
        }
        
        return dp[n];
    }

DP2

//25ms
    public int numSquares(int n) {
        int[] dp = new int[n + 1];
        Arrays.fill(dp, Integer.MAX_VALUE);
        
        dp[0] = 0;
        
        for(int i = 0; i <= n; i++) {
            for(int j = 1; i + j*j <= n; j++) {
                dp[i + j*j] = Math.min(dp[i] + 1, dp[i + j*j]);
            }
        }
        
        return dp[n];
    }

方法二:数学方法(不含证明)
四平方和定理,任何正整数均可表示为4个数以内的平方和。即返回结果只可能是1, 2, 3, 4中的一个。
如果一个数含有因子4,可以把4全部约掉,不影响结果。

下面用排除法:
如果一个数除以8余7,那么这个数为4个数的平方和。
把一个数拆成两个平方数之和,拆成功了返回1或2。其中一个数为0时返回1,都大于0返回2。
剩下的情况返回3。

//1ms
    public int numSquares(int n) {
        while(n % 4 == 0) {
            n /= 4;
        }
        
        if(n % 8 == 7) {
            return 4;
        }
        
        for(int a = 0; a*a <= n; a++) {
            int b = (int)Math.sqrt(n - a*a);
            if(a*a + b*b == n) {
                if(a > 0 && b > 0) {
                    return 2;
                } else if(a > 0 || b > 0) {
                    return 1;
                }
            }
        }
        
        return 3;
    }
【YOLO11改进 - 注意力机制】SCSA通过结合空间注意力和通道注意力,提高各种下游视觉任务的性能。 阅读详情

相关推荐

【python快速编程入门(第二版)黑马程序员课后编程题】

python快速编程入门(第二版)黑马程序员课后编程题

weixin_46167259的博客 1万+

[Leetcode] Perfect Squares 完美平方数

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. 动态规划 复杂度 时.

qq_36588760的博客 433

MQTT阿里云OTA 调试记录

2.验证,这个验证是什么意思呢,就是阿里云会把你包的信息,比如包大小,streamid(相当于包的识别码),版本号发送给你的设备,然后如果你没发送升级成功的包给阿里云,这里会是一直验证中的状态,那你的设备每次重新连接阿里云,发送connect包给阿里云的时候,阿里云就会发送一次这个验证(upgrade)包。提取到了之后就可以发送请求包了,这个请求包比较坑,也可能是我没付费,他的包一次最多只有1024个字节的数据。之前说了,验证状态下,设备发送connect包给阿里云,阿里云就会发一个upgrade包给你。

qq_42585211的博客 1237

一、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;...

a757259154的博客 447

无需UMAT:基于Abaqus内置模型实现形状记忆聚合物结构的热-力耦合仿真

本文详细介绍了如何利用Abaqus内置模型实现形状记忆聚合物(SMP)的热-力耦合仿真,无需编写复杂的UMAT子程序。通过广义Maxwell模型和Neo-Hookean模型的组合应用,结合完整的热-力耦合分析流程,帮助工程师高效模拟SMP的形状记忆行为。文章还提供了工程应用案例和常见问题解决方案,为相关领域的研究和设计提供实用指导。

weixin_34005090的博客 120

「 网络安全常用术语解读 」软件成分分析SCA详解:从发展背景到技术原理再到业界常用检测工具推荐

软件成分分析(Software Composition Analysis,SCA)是一种用于识别和分析软件内部组件及其关系的技术,旨在帮助开发人员更好地了解和管理其软件的构建过程,同时可帮助安全人员揭秘软件内部结构的神秘面纱。SCA技术的发展与软件行业的快速发展密不可分,下面将介绍SCA的发展背景、技术原理以及当前主流SCA工具。

所有成功的背后,都是痛苦的坚持,所有的痛苦,都是傻瓜般的不放弃! 5652

LeetCode 279. 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. Example 1: Input: n = 12 Output: 3 Explanation: 12 = 4 + 4 + 4. Example...

Dby_freedom的博客 614

【无标题】EXCEL实现刷题

运用Excel开发的刷题工具,具有普遍实用性,可将WORD的试题自动导入并实现刷题。

weixin_44081794的博客 2998

279. 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

Nick 1752

4. PCIe 接口时序

在上一篇中,大致介绍IPcore接口。总的来说接口如下图数据的接收和发送通过transmitTLPinterface和ReceiveTLPinterface。收发数据的时序也只是描述这几个接口的关系。从手册上截取PCIex1的接口时序。

jjinl的专栏 6138

MediaPipe+Unity动作捕捉实战:如何用普通摄像头驱动3D人物模型(附避坑指南)

本文详细介绍了如何利用MediaPipe和Unity实现低成本动作捕捉,仅需普通摄像头即可驱动3D人物模型。通过MediaPipe的3D坐标预测能力,结合Unity的骨骼映射优化,解决单目摄像头下的三维重建难题,并提供实战避坑指南和性能优化技巧,帮助开发者快速实现专业级的人物模型驱动效果。

weixin_29047545的博客 371

【算法】【数学】279. 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. Example 1: Input: n = 12 Output: 3 Explanation: 12 = 4 + 4 + 4. Example 2: Input: n = 13 Output: 2 Explanation: 13 = 4 + 9. 来源:

weixin_44420980的博客 321

智能车图像处理9-进阶篇1--中线的提取

(1)10-50行 基础扫线 (2)56-71 断行0 ,就是比如遇到十字,十字路口下方 第一次断线的地方。 (3)83-124 按照原先有效行,拟线作为中值,然后从中值向左右扫线,同时计算赛道宽度。 (4)125-138 如果出现赛道宽度小于先前有效行,则找到二次扫线的起点,从二次扫线起点处,继续向下扫线,直至结束,同时记录最后的有效行。 (5) 310-- 最后计算左右边线类似于方差的值,用于环岛的判断 ***这里着重描述一下二次扫线,希望大家可以理解的深刻

taiyuezyh的博客 1万+

leetcode 279. Perfect Squares-完美平方数|动态规划

原题链接:279. Perfect Squares 【思路-Java】 采用动态规划实现。用 dp[i] 数组存储第 i 个数的完美平方数。递推式为:dp[i] = Math.max(dp[j] + dp[i-j], dp[i],认为 i 的完全平方数是从和为 i 的两个完全平方数 dp[j] 和 dp[i-j]之和,然后从中取最小。 public class Solution {

hdtopku 1万+

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

LeetCode-279. Perfect Squares | (JAVA) (完美平方数)

279. 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.

mine_song的博客 1828

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

leetcode题解:第279题Perfect Squares

题目 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. Example1 Input: n = 12 Output: 3 Explanation: 12 = 4 +...

chenf1999的博客 284

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

[LeetCode] 279. Perfect Squares 完全平方数

[LeetCode] 279. Perfect Squares 完全平方数 问题描述:给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, …)使得它们的和等于 n。你需要让组成和的完全平方数的个数最少。 示例 1: 输入: n = 12 输出: 3 解释: 12 = 4 + 4 + 4. 示例 2: 输入: n = 13 输出: 2 解释: 13 = 4 + 9. 1. 数...

九霄星河的博客 3440

【LeetCode】279. Perfect Squares

279. Perfect Squares Description: Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n. Example 1: Input: n = 12 Output: 3...

Microstrong 2078

【Leetcode】279. 完全平方数(Perfect Squares)

Leetcode - 279 Perfect Squares (Medium) 题目描述:给定一个正整数,求最少能由几个完全平方数组成。 Input: n = 13 Output: 2 Explanation: 13 = 4 + 9. 解题思路:定义状态 dp[n] 表示 n 的最少完全平方数,状态方程为 dp[n] = Min{ dp[n - i*i] + 1 }。 dp[0] = 0 dp...

厉兵秣码 307

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, g

James Pan 2374

279. Perfect Squares

Given a positive integern, find the least number of perfect square numbers (for example,1, 4, 9, 16, ...) which sum ton. Example 1: Input: n = 12 Output: 3 Explanation: 12 = 4 + 4 + 4. Example...

hello world 267

LeetCode-279. Perfect Squares [C++][Java]

Given an integern, returnthe least number of perfect square numbers that sum ton. Aperfect squareis an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example,1,4,9, and16are perfect s

贫道绝缘子的博客 2920

leetcode 279. Perfect Squares (python)

leetcode 279. Perfect Squares题目解法1:DP解法2:BFS算法流程解法3:lagrange四平方和定理 题目 Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, …) which sum to n. Example ...

qq_37821701的博客 1230

LeetCode - 279. Perfect Squares

题目 LeetCode - 279. Perfect Squares 题目链接 https://leetcode.com/problems/perfect-squares/ 参考博客 https://www.cnblogs.com/grandyang/p/4800552.html 解题思路 本题解法参考上述博客。 1. 解法一:四平方和定理 根据四平方和定理知,任何正整数可表示成四个整数的平方和。...

virgilshi的博客 224

LeetCode //C - 279. Perfect Squares

【代码】LeetCode //C - 279. Perfect Squares。

Made in Code 903

LeetCode-279. Perfect Squares

Description Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n. Example 1 Input: n = 12 Output: 3 Explanation: 12 = 4 + ...

zy2317878的博客 4183
上一篇: leetcode 264. Ugly Number II(丑数 2)
下一篇: leetcode 300. Longest Increasing Subsequence(最长递增子序列)
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值
⌂ 换网址