leetcode@ [279]Perfect Squares
2015-11-02 14:11
351 查看
https://leetcode.com/problems/perfect-squares/
Given a positive integer n, find the least number of perfect square numbers (for example,
For example, given n =
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
3because
12 = 4 + 4 + 4; given n =
13, return
2because
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 == 0){ return 0; } int next = getMaximumSquare(load); int Min = numeric_limits<int>::max(); for(int v=next;v>=1;v--){ if(load-v*v >= 0){ Min = min(Min, dfs(load-v*v, dp)); } } dp[load] = Min + 1; return dp[load]; } int numSquares(int n) { vector<int> dp(n+1); for(int i=0;i<dp.size();++i) dp[i] = 0; dp = dfs(n, dp); return dp ; } };
相关文章推荐
- EXTJS常用设置(持续更新)
- 【Feature To Polygon】行政边界地图缺少某个面要素的生成方法
- 浅试WebStorm配置Node.js开发环境
- JSONP和AJAX不同过程的详细分析
- js中中文逗号与英文逗号的互相转换
- 使用jsoup解析HTML之jsoup和HTML DOM简介
- JavaScript中类和构造函数的理解
- 百度地图API示例 JS
- 用JavaScript实现歌词滚动播放
- 关注jquery技巧提高jquery技能(前端开发必学)
- HTML5网页录音和压缩
- h5 轮播图
- 一个对前端模板技术的全面总结
- 2015前端框架何去何从
- js 去除字符串中间的空格
- Jsp的九个隐含对象
- React 的 diff 算法
- Git中的AutoCRLF与SafeCRLF换行符问题
- jquery Tab默认情况下自动切换
- 解决手机访问页面字体太小问题