Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Leetcode 279. Perfect Squares

Leetcode 279. Perfect Squares

作者头像
triplebee
发布于 2018-03-27 08:57:22
发布于 2018-03-27 08:57:22
71000
代码可运行
举报
运行总次数:0
代码可运行

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.

计算一个数最少由多少个完全平方数相加而成。

简单DP,从1开始向上转移。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class Solution {
public:
    int numSquares(int n) {
        vector<int> dp(n + 1, INT_MAX);
        dp[0] = 0;
        for(int i = 1; i <= n; i++ )
            for(int j = 1; j <= sqrt(i); j++)
                dp[i] = min(dp[i], dp[i - j * j] + 1);
        return dp[n];
    }
};
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
Dynamic Programming - 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.
ppxai
2020/09/23
4120
LeetCode 279. Perfect Squares
题目 动态规划 class Solution { public: int dp[100005]; int numSquares(int n) { if(n==0) return 0; dp[0]=0; for(int i=1;i<=n;i++) { dp[i]=INT_MAX; for(int j=1;j*j<=i;j++)
ShenduCC
2020/05/12
3240
【LeetCode热题100】【动态规划】完全平方数
所以题目变成要从1,2,3,……,n的平方根中找出平方和的和是n的组合,并且数量最少
叶茂林
2024/04/18
1240
LeetCode 0279 - 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.
Reck Zhang
2021/08/11
2140
画解算法:279. 完全平方数
https://leetcode-cn.com/problems/perfect-squares/
灵魂画师牧码
2019/08/01
1.2K0
画解算法:279. 完全平方数
【超直白】leetcode 279 完全平方数
给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, …)使得它们的和等于 n。你需要让组成和的完全平方数的个数最少。
未名编程
2024/10/12
1460
Leetcode 题目解析之 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.
ruochen
2022/01/15
1.3K0
LeetCode 279. 完全平方数(DP)
给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, …)使得它们的和等于 n。你需要让组成和的完全平方数的个数最少。
Michael阿明
2020/07/13
6990
LeetCode 279. 完全平方数(DP)
LeetCode279:完全平方数,动态规划解法超过46%,作弊解法却超过97%
本篇概览 这是道高频面试题,值得一看 首先,这道题的难度是中等 来看题目描述: 给你一个整数 n ,返回 和为 n 的完全平方数的最少数量 。 完全平方数 是一个整数,其值等于另一个整数的平方;换句话说,其值等于一个整数自乘的积。例如,1、4、9 和 16 都是完全平方数,而 3 和 11 不是。 示例1: 输入:n = 12 输出:3 解释:12 = 4 + 4 + 4 示例 2: 输入:n = 13 输出:2 解释:13 = 4 + 9 提示: 1 <= n <= 104 解题思路 该题的解题思路是
程序员欣宸
2022/09/29
4540
LeetCode279:完全平方数,动态规划解法超过46%,作弊解法却超过97%
力扣279——完全平方数
给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...)使得它们的和等于 n。你需要让组成和的完全平方数的个数最少。
健程之道
2020/02/12
4990
【leetcode刷题】T164-完全平方数
给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, …)使得它们的和等于 n。你需要让组成和的完全平方数的个数最少。
木又AI帮
2019/09/16
4040
279. 完全平方数
给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, …)使得它们的和等于 n。你需要让组成和的完全平方数的个数最少。
张伦聪zhangluncong
2022/10/26
4960
动态规划高频题
Given an unsorted array of integers, find the length of longest increasing subsequence.
王脸小
2019/10/18
6040
Leetcode【279、343】
这道题实际上和 Leetcode 【DP、BFS】322. Coin Change 很相似。我们将 <= n 的平方数因子当作硬币种类数,n 当作需要换的零钱,则可以使用相同的方法,即 DP 和 BFS 来求解。
echobingo
2019/07/22
5690
LeetCode-279-完全平方数
给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...)使得它们的和等于 n。你需要让组成和的完全平方数的个数最少。
benym
2022/07/14
2650
本周小结!(动态规划系列五)
动态规划:377. 组合总和 Ⅳ中给定一个由正整数组成且不存在重复数字的数组,找出和为给定目标正整数的组合的个数(顺序不同的序列被视作不同的组合)。
代码随想录
2021/02/26
6480
LintCode 完美平方题目分析代码
给一个正整数 n, 找到若干个完全平方数(比如1, 4, 9, ... )使得他们的和等于 n。你需要让平方数的个数最少。
desperate633
2018/08/22
4920
【动态规划算法练习】day16
DP42 【模板】完全背包 本题目来源于牛客网,大家可以点开上面的链接,进入题目页面进行练习。
摘星
2023/10/15
1820
【动态规划算法练习】day16
八十九、动态规划系列背包问题之完全背包
动态规划需要搞定三个系列:三个背包,零钱问题和股票问题。今天就开始干掉三个背包问题。
润森
2022/08/17
3490
八十九、动态规划系列背包问题之完全背包
C#版[击败100%的提交] - Leetcode 279. 完全平方数 - 题解
在线提交: https://leetcode.com/problems/perfect-squares/
Enjoy233
2019/03/05
7710
相关推荐
Dynamic Programming - 279. Perfect Squares
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验