要求

  • 给出一个正整数n,寻找最少的完全平方数,使他们的和为n

示例

  • n = 12
  • 12 = 4 + 4 + 4
  • 输出:3

边界

  • 是否可能无解

思路

  • 贪心:12=9+1+1+1,无法得到最优解
  • 图论:从n到0,每个数字表示一个节点,如果两个数字x到y相差一个完全平方数,则连接一条边
  • 问题转化为无权图中从n到0的最短路径

  

实现

  • 队列中每个元素是一个pair对,保存具体数字和经历了几段路径走到这个数字
 1 class Solution {
2 public:
3 int numSquares(int n) {
4
5 assert( n > 0 );
6
7 queue< pair<int,int> > q;
8 q.push( make_pair( n , 0 ) );
9
10 while( !q.empty() ){
11 int num = q.front().first;
12 int step = q.front().second;
13 q.pop();
14
15 if( num == 0 )
16 return step;
17
18 for( int i = 1 ; num - i*i >=0 ; i ++ )
19 q.push( make_pair( num - i * i , step + 1 ) );
20 }
21
22 throw invalid_argument("No Solution");
23 }
24 };
  • 有些节点被重复推入队列,n足够大时存在性能问题
  • 不同于树,图中每个节点都有多种路径到达

优化

  • 用一个辅助向量 visited 记录节点是否推入过队列
  • 用变量记录num-i*i,减少计算
  • 推入0时,直接返回结果,而不需要在循环处先取出
 1 class Solution {
2 public:
3 int numSquares(int n) {
4
5 assert( n > 0 );
6
7 queue< pair<int,int> > q;
8 q.push( make_pair( n , 0 ) );
9
10 vector<bool> visited(n+1, false);
11 visited[n] = true;
12
13 while( !q.empty() ){
14 int num = q.front().first;
15 int step = q.front().second;
16 q.pop();
17
18 for( int i = 1 ; ; i ++ ){
19 int a = num - i*i;
20 if( a < 0 )
21 break;
22 if( a == 0)
23 return step + 1;
24 if( ! visited[a] ){
25 q.push( make_pair( a , step + 1 ) );
26 visited[a] = true;
27 }
28 }
29 }
30 throw invalid_argument("No Solution");
31 }
32 };

相关

  • 127 Word Ladder
  • 126 Word Ladder II

[刷题] 279 Perfect Squares的更多相关文章

  1. 花式求解 LeetCode 279题-Perfect Squares

    原文地址 https://www.jianshu.com/p/2925f4d7511b 迫于就业的压力,不得不先放下 iOS 开发的学习,开始走上漫漫刷题路. 今天我想聊聊 LeetCode 上的第2 ...

  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 完全平方数

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

  4. 279. Perfect Squares

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

  5. leetcode@ [279]Perfect Squares

    https://leetcode.com/problems/perfect-squares/ Given a positive integer n, find the least number of ...

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

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

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

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

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

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

  9. 279 Perfect Squares 完美平方数

    给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...) 使得他们的和等于 n.你需要让平方数的个数最少.比如 n = 12,返回 3 ,因为 12 = 4 + 4 + 4 : ...

随机推荐

  1. vmstat-观察进程上线文切换

    vmstat 是一款指定采样周期和次数的功能性监测工具,我们可以看到,它不仅可以统计内存的使用情况,还可以观测到 CPU 的使用率.swap 的使用情况.但 vmstat 一般很少用来查看内存的使用情 ...

  2. vue-cli2 生成的项目打包优化(持续学习中)

    1.昨天看到自己的项目每次打包后都是30M左右,就觉得这个打包后的dist文件太大了,能不能小点呢, 然后就看网上的资料,提供了好多优化的办法,但是我只用了一个,后期再不断的优化吧. 打开工程项目文件 ...

  3. [状压DP]车

    车 车 车 题目描述 在 n ∗ n n*n n∗n( n ≤ 20 n≤20 n≤20)的方格棋盘上放置 n n n个车(可以攻击所在行.列),有些格子不能放,求使它们不能互相攻击的方案总数. 输入 ...

  4. day-06-集合-缓存机制-深浅copy

    (1) is id ==用法 is 判断的是内存地址是否相同 id 查看内存地址:id相同,值一定相同,值相同,id不一定相同 == 比较判断是否相等 l1 = [1, 2, 3] l2 = [1, ...

  5. 利用查询条件对象,在Asp.net Web API中实现对业务数据的分页查询处理

    在Asp.net Web API中,对业务数据的分页查询处理是一个非常常见的接口,我们需要在查询条件对象中,定义好相应业务的查询参数,排序信息,请求记录数和每页大小信息等内容,根据这些查询信息,我们在 ...

  6. jdbcTemplate事务管理

    1.基于TransactionTemplate的编程式事务管理 Spring之路(39)–基于TransactionTemplate的编程式事务管理 本篇通过TransactionTemplate类, ...

  7. React函数式组件的性能优化

    优化思路 主要优化的方向有2个: 减少重新 render 的次数.因为在 React 里最重(花时间最长)的一块就是 reconction(简单的可以理解为 diff),如果不 render,就不会 ...

  8. JavaScript设计模式(二):工厂模式

    工厂模式模式的定义与特点 工厂模式(Factory Pattern)是编程中最常用的设计模式之一.这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式.在工厂模式中,我们在创建对象时不会对 ...

  9. 【pytest官方文档】解读fixtures - 11. fixture的执行顺序,3要素详解(长文预警)

    当pytest要执行一个测试函数,这个测试函数还请求了fixture函数,那么这时候pytest就要先确定fixture的执行顺序了. 影响因素有三: scope,就是fixture函数的作用范围,比 ...

  10. 关于Oracle 数据库使用dba_tables或者all_tables或者user_tables统计数据时,与直接查询表统计时数据不一致的记录

    1. 今天写代码发现这个问题,这里记录一下, 不一致的原因是因为  dba_tables .all_tables.user_tables 不是实时的反应表的数据的,所以需要在查询统计之前对表进行手动分 ...