当前位置:网站首页>279. Complete square
279. Complete square
2022-07-18 12:08:00 【Mr Gao】
279. Complete square
Give you an integer n , return And for n The minimum number of complete squares of .
Complete square It's an integer , Its value is equal to the square of another integer ; let me put it another way , Its value is equal to the product of the self multiplication of an integer . for example ,1、4、9 and 16 They're all perfect squares , and 3 and 11 No .
Example 1:
Input :n = 12
Output :3
explain :12 = 4 + 4 + 4
Example 2:
Input :n = 13
Output :2
explain :13 = 4 + 9
We can write the state expression according to the requirements of the topic :f[i]f[i]f[i] Represents the minimum number of squares needed to represent an integer iii.
These numbers must fall in the interval [1,n] [1,sqrt{n}][1,n]. We can enumerate these numbers , Suppose the current enumeration goes to jjj, Then we need to take the square of some numbers , constitute i−j2i-j^2i−j2. At this point, we find that the subproblem is similar to the original problem , It's just getting smaller . This meets the requirements of dynamic programming , So we can write the state transition equation .
For this question , In fact, we use the idea of dynamic programming , Or search from the beginning , about , Every number , We get its complete square number from scratch , Then add up one by one , Add to n, This allows for all combinations , The solution code is as follows :
int numSquares(int n){
int dp[n+1];
int i,j;
dp[0]=0;
for(i=1;i<=n;i++){
dp[i]=INT_MAX;
for(j=1;j<=sqrt(i);j++){
dp[i]=fmin(dp[i],dp[i-j*j]+1);
}
// printf("i %d -%d ",i,dp[i]);
}
return dp[n];
}
边栏推荐
- 传统健身房困于规模化,乐刻运动“S2B2C”模式成参考答案?
- Kettle【实践 01】Linux环境下使用Azkaban定时调用Kettle的KJB或KTR脚本实现自动化数据处理(完整流程实例分享:包含sql+ktr+shell+flow相关文件云资源)
- mysql原字段转驼峰命名
- GDB common instructions
- Flink基础记录补充
- IC classification of speech chip and comparison and selection of OTP speech chip of sop8
- Solve the "cannot find module 'path' or its corresponding type declarations." in TS
- openEuler 知:repo
- [reprint] summary of spaces in latex
- Openeuler knowledge: common website
猜你喜欢

UE5簡單的角色碰撞檢測功能

2022年成都/杭州/厦门/武汉产品经理认证招生简章(NPDP)

Power bi---- DAX explanation

Ftxui basic notes (Hello World)

JupyterLab安装

Overview of database system -- overview of data model

Differences among screenwidth, clientwidth, offsetwidth, and scrollwidth

Solution: referenceerror: PubSub is not defined

Ue5 fonctions simples de détection des collisions de rôles

Jupyterab installation
随机推荐
YGG established a new subdao - YGG Japan
Flink基础记录补充
Strings containing numbers are eliminated and letters are incremented according to the step size
力扣(LeetCode)196. 删除重复的电子邮箱(2022.07.15)
[load balancer does not contain an instance for the service mall coupling] and the project start normally but cannot register with Nacos
Heavyweight: Mobileye officially announced the postponement of IPO, slowing down the growth of revenue and intensifying market competition
Why do consumers buy iPhones instead of domestic ones? Because the depreciation of domestic mobile phones is too fast
Common audio features: Mel spectrum, amplitude spectrum (short time Fourier transform spectrum /stft), Mel cepstrum (MFCC)
自动推理的逻辑02-命题微积分
【Jailhouse 文章】Bao: A Lightweight Static Partitioning Hypervisor for Modern Multi-Core Embedded...
uniapp uni-popup change
Openeuler knowledge: sig
[object conversion] vo2dto use
圖片格式解析
[object header] view the bytes occupied by the object
Add Tsinghua image in CONDA
社区峰会|Pulsar Summit 旧金山峰会议题亮点曝光!
Web 编程面试题(2022)
Opensource knowledge: embedded software list
TMUX usage