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.
解题思路
利用动态规划思想解题,初始化dp数组令小于n的完全平方数为1,从1到n遍历求解最小组成个数,再对每个数遍历小于其的所有完全平方数,最小组成个数的状态转移方程为:
dp[i] = min(dp[i], dp[i - j * j] + 1)
代码
class Solution {
public:
int numSquares(int n) {
vector<int> dp(n + , INT_MAX);
for(int i = ; i * i <= n; i++)
dp[i * i] = ;
for(int i = ; i <= n; i++)
for(int j = ; j * j < i; j++)
dp[i] = min(dp[i], dp[i - j * j] + );
return dp[n];
}
};
LeetCode 279. 完全平方数(Perfect Squares)的更多相关文章
- LeetCode 279. 完全平方数(Perfect Squares) 7
279. 完全平方数 279. Perfect Squares 题目描述 给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...)使得它们的和等于 n.你需要让组成和的完全平方数 ...
- Java实现 LeetCode 279 完全平方数
279. 完全平方数 给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, -)使得它们的和等于 n.你需要让组成和的完全平方数的个数最少. 示例 1: 输入: n = 12 输出: ...
- LeetCode(279)Perfect Squares
题目 Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9 ...
- [Swift]LeetCode279. 完全平方数 | Perfect Squares
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...
- [LeetCode] 279. 完全平方数(DP)
###题目 给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...)使得它们的和等于 n.你需要让组成和的完全平方数的个数最少. 示例 1: 输入: n = 12 输出: 3 解 ...
- LeetCode OJ:Perfect Squares(完美平方)
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...
- Leetcode 279. 完全平方数
题目描述: https://leetcode-cn.com/problems/perfect-squares/ 解题思路: 同样是dp,一开始的想法是,对于每个数i做拆分为j和(i-j),利用动态转移 ...
- Leetcode之广度优先搜索(BFS)专题-279. 完全平方数(Perfect Squares)
Leetcode之广度优先搜索(BFS)专题-279. 完全平方数(Perfect Squares) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ar ...
- 花式求解 LeetCode 279题-Perfect Squares
原文地址 https://www.jianshu.com/p/2925f4d7511b 迫于就业的压力,不得不先放下 iOS 开发的学习,开始走上漫漫刷题路. 今天我想聊聊 LeetCode 上的第2 ...
随机推荐
- HTML Ueditor加载空白问题
问题描述 Ueditor打开时加载不出内容 原因分析 Ueditor重复加载时,会存在缓存问题 Ueditor采用异步加载方式,所以数据获取和赋值要写在Ueditor异步回调里 解决方案 UE.del ...
- 微信小程序异步回调
场景如下:现有一个方法需要等待其他N个异步函数执行完毕后执行,callback麻烦的头大,翻了一波API原来小程序已经支持 async函数,那一切就好办了. 废话不多说,直接开始撸... 第一步:打开 ...
- bash shell脚本之成员变量
shell中变量的使用 cat test3: #!/bin/bash # testing variables days= guest="Katie" echo "$gue ...
- spring 整合mongodb报NoSuchMethodError错误
刚开始通过网上查到相关的资料进行了一些配置,参考链接:http://www.open-open.com/lib/view/open1454374782167.html maven的dependenci ...
- etcd安装和简单使用
etcd作为一个高可用强一致性的服务发现存储仓库,在Kubernetes等开源项目中用的很多,这里简单记录下安装和常用命令以及api 安装 安装包可以从 https://github.com/etcd ...
- C# Winfrom 自定义控件添加图标
Winfrom自定义控件添加自定义图标实现方式: 1.新建UserControl——略 2.寻找合适的图标文件——将文件和控件放置同一目录下(相同目录.自定义控件类名.图标文件名相同) 注:如果路径不 ...
- PAT乙级1037
题目链接 https://pintia.cn/problem-sets/994805260223102976/problems/994805284923359232 题解 还算简单,就是模拟我们在生活 ...
- vue store获取值时 Computed property "activeTag" was assigned to but it has no setter.
出现原因: element-ui中 el-tab绑定的值在切换tab时会自动修改 而activeTag是从store中获取的值,不能直接修改 要添加给它绑定上set <el-tabs cla ...
- linux基础_使用指令3
时间日期类 1.date指令 功能:显示当前日期 语法: date:显示当前时间 date +%Y:显示当前年份 date +%m:显示当前月份 date +%d:显示当前是哪一天 date &quo ...
- c#使用 StackExchange.Redis 封装 RedisHelper
公司一直在用.net自带的缓存,大家都知道.net自带缓存的缺点,就不多说了,不知道的可以查一查,领导最近在说分布式缓存,我们选的是redis,领导让我不忙的时候封装一下,搜索了两天,选了选第三方的插 ...