当前位置:网站首页>Perfect squares for leetcode topic analysis
Perfect squares for leetcode topic analysis
2022-06-23 06:03:00 【ruochen】
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.
Dynamic programming , Solve the optimization problem , Bottom up .
public int numSquares(int n) {
int[] dp = new int[n + 1];
Arrays.fill(dp, Integer.MAX_VALUE);
// Set all squares 1
for (int i = 0; i * i <= n; i++) {
dp[i * i] = 1;
}
for (int a = 1; a <= n; a++) {
for (int b = 1; a + b * b <= n; b++) {
// Take the smaller value ,a + b * b It can also be a square number
dp[a + b * b] = Math.min(dp[a] + 1, dp[a + b * b]);
}
}
return dp[n];
}边栏推荐
- Real MySQL interview questions (XXVI) -- didi 2020 written examination questions
- The difference between SaaS software and traditional software delivery mode
- iNFTnews | 加密之家从宇宙寄来的明信片,你会收到哪一张?
- android Handler内存泄露 kotlin内存泄露处理
- Pyqt5 设置窗口左上角图标
- PAT 乙等 1014 C语言
- Pat class B 1026 program running time
- Pat class B 1019 C language
- [cocos2d-x] screenshot sharing function
- True MySQL interview question (XXII) -- condition screening and grouping screening after table connection
猜你喜欢

如何指定pig-register项目日志的输出路径

Data migration from dolphin scheduler 1.2.1 to dolphin scheduler 2.0.5 and data test records after migration

【Cocos2d-x】自定义环形菜单

雷达图canvas

内存分析与内存泄漏检测

Dolphin scheduler dolphin scheduling upgrade code transformation -upgradedolphin scheduler

技术开发团队视角看到的数字藏品机遇与挑战

编址和编址单位

【Cocos2d-x】可擦除的Layer:ErasableLayer

jvm-06.垃圾回收器
随机推荐
Huawei's software and hardware ecosystem has taken shape, fundamentally changing the leading position of the United States in the software and hardware system
数字藏品如何赋能经济实体?
The digital collection market has just begun
Explicability of counter attack based on optimal transmission theory
Operating mongodb in node
PAT 乙等 1024 科学记数法 C语言
Data migration from dolphin scheduler 1.2.1 to dolphin scheduler 2.0.5 and data test records after migration
Raspberry pie assert preliminary exercise
PAT 乙等 1021 个位数统计
Pat class B 1010 C language
[image fusion] sparse regularization based on non convex penalty to realize image fusion with matlab code
PAT 乙等 1015 C语言
Adnroid activity screenshot save display to album view display picture animation disappear
Excel sheet column title for leetcode Title Resolution
Implementation of linear list linked list structure
Wireshark TS | video app cannot play
Oracle exception
PAT 乙等 1012 C语言
Advanced Mathematics (Seventh Edition) Tongji University exercises 1-7 personal solutions
Memory analysis and memory leak detection