一维动态规划总结

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_28350997/article/details/82861162

题目列表

给一个N(输入),求某种情况的最大值或者最小值情况,
279. Perfect Squares

思路

最差情况下,总体是定义一个dp[N+1], 或者初始化前面dp[0]或者dp[1],

#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.

Example 1:

Input: n = 12
Output: 3
Explanation: 12 = 4 + 4 + 4.
Example 2:

Input: n = 13
Output: 2
Explanation: 13 = 4 + 9.

将一个数据进行拆分,因子为 1,2,3,4,的平方

解法

如果一个数x可以表示为一个任意数a加上一个平方数bxb,也就是x=a+bxb,那么能组成这个数x最少的平方数个数,就是能组成a最少的平方数个数加上1(因为b*b已经是平方数了)。

猜你喜欢

转载自blog.csdn.net/qq_28350997/article/details/82861162