https://leetcode.com/problems/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.

For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 13, return 2 because 13 = 4 + 9.

class Solution {
public:
int getMaximumSquare(int n){
int ret = (int)sqrt(n);
return ret;
}
int dfs(int load, vector<int> &dp){
if(dp[load]) return dp[load];
if(load == ){
return ;
} int next = getMaximumSquare(load);
int Min = numeric_limits<int>::max();
for(int v=next;v>=;v--){
if(load-v*v >= ){
Min = min(Min, dfs(load-v*v, dp));
}
}
dp[load] = Min + ;
return dp[load];
}
int numSquares(int n) {
vector<int> dp(n+);
for(int i=;i<dp.size();++i) dp[i] = ; dp[n] = dfs(n, dp);
return dp[n];
}
};
 

leetcode@ [279]Perfect Squares的更多相关文章

  1. [LeetCode] 279. Perfect Squares 完全平方数

    Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...

  2. (BFS) leetcode 279. Perfect Squares

    Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...

  3. [leetcode] #279 Perfect Squares (medium)

    原题链接 题意: 给一个非整数,算出其最少可以由几个完全平方数组成(1,4,9,16……) 思路: 可以得到一个状态转移方程  dp[i] = min(dp[i], dp[i - j * j] + ) ...

  4. [LeetCode 279.] Perfect Squres

    LeetCode 279. Perfect Squres DP 是笨办法中的高效办法,又是一道可以被好办法打败的 DP 题. 题目描述 Given a positive integer n, find ...

  5. [LeetCode] 0279. Perfect Squares 完全平方数

    题目 Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9 ...

  6. 【LeetCode】279. Perfect Squares 解题报告(C++ & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 四平方和定理 动态规划 日期 题目地址:https: ...

  7. 【leetcode】Perfect Squares (#279)

    Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...

  8. 279. Perfect Squares

    Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...

  9. 279. Perfect Squares(动态规划)

    Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...

随机推荐

  1. (转)struts2.0配置文件、常量配置详解

    一.配置: 在struts2中配置常量的方式有三种: 在struts.xml文件中配置 在web.xml文件中配置 在sturts.propreties文件中配置 1.之所以使用struts.prop ...

  2. 在MAC下调试运行暗黑全世界客户端及部分代码注解(基于Firefly)

    原地址:http://www.myexception.cn/program/1399860.html 在MAC下调试运行暗黑全世界客户端及部分代码注解(基于Firefly) 在MAC下调试运行暗黑世界 ...

  3. javascript div z-index, input tabindex属性说明

    <html> <body> <form> 用户名: <input type="text" tabindex="1" / ...

  4. 【leetcode】Container With Most Water(middle)

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

  5. [Ruby on Rails系列]3、初试Rails:使用Rails开发第一个Web程序

    本系列前两部分已经介绍了如何配置Ruby on Rails开发环境,现在终于进入正题啦! Part1.开发前的准备 本次的主要任务是开发第一个Rails程序.需要特别指出的是,本次我选用了一个(Paa ...

  6. codeforces #313 div1 E

    首先我们要注意到一个事情 如果一个灯塔向左覆盖,那么比他小的某个灯塔如果向左覆盖的端点大于当前塔向左覆盖的端点,他一定向右覆盖 对于当前灯塔向右覆盖也是同理 那么我们只需要记录当前覆盖到的端点就可以完 ...

  7. Android Framework------之Input子系统

    下面这是基于Android4.2代码的关于Input子系统的笔记.在这篇笔记中,只涉及Android相关的东西,关于Linux内核中对各种输入设备的统一,在本文中不作说明.此外,由于才疏学浅,文中难免 ...

  8. Android开发之Bitmap.Config.RGB_565

    在学习xutils框架的时候,看到sample代码中有一行这样的代码: bitmapUtils.configDefaultBitmapConfig(Bitmap.Config.RGB_565); Bi ...

  9. [原]Unity3D深入浅出 - 粒子系统(Particle System)

    粒子系统是在三维空间渲染出来的二维图像,主要用于烟,火,水滴,落叶等效果.一个粒子系统由粒子发射器.粒子动画器和粒子渲染器三个独立的部分组成. Unity中自带了一些粒子效果,在Assets>I ...

  10. C#调用Geocoding API进行地理编码与逆编码

    使用C#调用Geocoding API来将地址转为经纬度,或者将经纬度转变为具体的地址. Geocoding API的详细介绍参见:http://developer.baidu.com/map/web ...