当前位置:网站首页>279.完全平方数
279.完全平方数
2022-06-23 05:06:00 【zzu菜】
完全平方数
给你一个整数 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
思考
这一题我觉得其实比我做的上一题 322. 零钱兑换 更加简单,
因为这个没有不存在的情况,因为有万能1的存在,怎么凑都可以凑够!
我认为这个只是多出一步需要自己求出,n以内的完全平方数,就可以转换成一个和322. 零钱兑换类似的问题,并且更简单考虑的情况更少!
首先第一个问题,我们如何求n以内完全平方数
// step 0 求n以内的完全平方数
int[] square=new int[n/2];
int l=1;
square[0] = 1;
for (int i=2;i<n;i++){
if(i*i>n) break;
square[i-1] = i*i;
l++;
}
解下来就转化成了01背包问题,假设n=12,其完全平方数数组为x={1,2,4,9}
其实也就是用{1,2,4,9}里面最少的数去凑满12即可!
定义dp数组及其下标含义
dp[j] : 表示使用了完全平方数组去凑满j使用的完全平方数的最少个数
初始化dp数组
dp[0]=0; 保证 dp[j-x[i]] x[i]==j时 dp[j] = 1;
dp[1]=1;
其余dp[j] = Integer.Max;
状态转移方程
遍历x,选择最小的dp[j] = dp[j - x[i]]
public int numSquares(int n) {
// step 0 求n以内的完全平方数
int[] square=new int[n/2];
int l=1;
square[0] = 1;
for (int i=2;i<n;i++){
if(i*i>n) break;
square[i-1] = i*i;
l++;
}
if(l<=1) return n;
// step 2 创建dp数组
int[] dp=new int[n+1];
// step 3 初始化dp数组
dp[0]=0;
dp[1]=1;
for (int i=2;i<dp.length;i++){
dp[i] =Integer.MAX_VALUE;
}
// step 4 完善dp数组
for (int j=2;j<dp.length;j++){
for (int num : square){
if(num<=j){
dp[j]=Math.min(dp[j],dp[j-num]+1);
}
}
}
// print Dp
for (int i=0;i<dp.length;i++){
System.out.println("dp "+i+","+dp[i]);
}
return dp[dp.length-1];
}
进阶
其获取>n的完全平方数,完全可以在循环中进行
public int numSquares2(int n) {
// step 2 创建dp数组
int[] dp=new int[n+1];
// step 3 初始化dp数组
dp[0]=0;
dp[1]=1;
for (int i=2;i<dp.length;i++){
dp[i] =Integer.MAX_VALUE;
}
// step 4 完善dp数组
for (int j=2;j<dp.length;j++){
//step 0 求n以内的完全平方数
for (int i=1;i*i<=j;i++){
int num=i*i;
if(num<=j){
dp[j]=Math.min(dp[j],dp[j-num]+1);
}
}
}
// step 5 print Dp
for (int i=0;i<dp.length;i++){
System.out.println("dp "+i+","+dp[i]);
}
return dp[dp.length-1];
}
边栏推荐
- 【已解决】“The Unity environment took too long to respond. Make sure that :\n“
- Leetcode topic resolution integer to Roman
- 基于T5L1的小型PLC设计方案
- Matplotlib savefig multiple picture overlay
- Jour 04 projet de santé mentale - gestion des rendez - vous - gestion des forfaits
- Cryptography series: certificate format representation of PKI X.509
- mongodb项目中可能出现的坑
- JVM原理简介
- Given a node of a binary tree, return the successor node of the node
- Fastdata pole | insight report on e-commerce consumption of young Chinese users 2021
猜你喜欢
百度URL參數之LINK?URL參數加密解密研究(代碼實例)

华为软件测试笔试真题之变态逻辑推理题

In the half year summary, it people just want to lie flat
![[cocos2d-x] custom ring menu](/img/fd/c18c39ae738f6c1d2b76b6c54dc654.png)
[cocos2d-x] custom ring menu

Tcp/ip explanation (version 2) notes / 3 link layer / 3.4 bridge and switch

Introduction to JVM principle

Day_07 传智健康项目-Freemarker

Day_04 傳智健康項目-預約管理-套餐管理

Day_ 01 smart communication health project - project overview and environmental construction

Day_13 傳智健康項目-第13章
随机推荐
Dora's Google SEO tutorial (1) SEO novice guide: establishment of preliminary optimization thinking
Shutter style
Find the number of nodes in the widest layer of a binary tree
什么是PDCA循环?如何整合 PDCA 循环和 OKR
Docker实战 -- 部署Redis集群与部署微服务项目
Multiple strings for leetcode topic resolution
(1)基础学习——vim编辑器常用快捷操作命令
WordPress aawp 3.16 cross site scripting
Illuminate\Support\Collection 去重 unique 列表去重
Link of Baidu URL Parameters? Recherche sur le chiffrement et le décryptage des paramètres d'URL (exemple de Code)
How to query fields separated by commas in MySQL as query criteria - find_ in_ Set() function
Xray linkage crawlergo automatic scanning pit climbing record
Pyinstaller sklearn reports errors
Day_04 传智健康项目-预约管理-套餐管理
Redis 哨兵
Day_ 02 smart communication health project - appointment management - inspection item management
Leetcode topic resolution valid Sudoku
Network packet capturing tcpdump User Guide
Laravel log channel 分组配置
mysql以逗号分隔的字段作为查询条件怎么查——find_in_set()函数