要求

  • 给出一个正整数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. docker部署nodejs项目应用

    之前笔者弄了一套nestjs项目放在自己服务器上,并用pm2管理进程. 现在要把pm2停止,尝试一下用docker容器,那么首先要安装docker 一.安装docker 由于笔者服务器的系统是cent ...

  2. OpenCV 之 平面单应性

    上篇 OpenCV 之 图象几何变换 介绍了等距.相似和仿射变换,本篇侧重投影变换的平面单应性.OpenCV相关函数.应用实例等. 1  投影变换 1.1  平面单应性 投影变换 (Projectiv ...

  3. 导出目录的JS代码,与目录的三级标题测试

    二级标题 三级标题 三级标题 三级标题 三级标题 三级标题 二级标题 三级标题 三级标题 三级标题 三级标题 三级标题 这里是现在页尾目录功能的代码源码: <!-- 目录索引列表生成 --> ...

  4. rancher的ssl部署

    前言 因为我司有多套k8s环境,管理起来过于麻烦,需要一个统一的管理平台,又因为没有预留时间自己开发,经过选择后,使用rancher来进行多k8s环境的统一管理平台. 部署 1.在阿里云上申请免费的证 ...

  5. ionic3 StatusBar 不显示问题

    import { StatusBar } from '@ionic-native/status-bar'; constructor(private statusBar: StatusBar) { } ...

  6. windows2003安装php ,mysql,fastgui

    在上一章中,windows2003的iis搭建已经完成,但是我们现在用的多的也包含php,该如何让Windows2003成功使用php文件呢? windows2003需要先行安装vc9运行库才能与fa ...

  7. Spring(七)SpringMVC的文件上传

    1-SpringMVC的请求-文件上传-客户端表单实现(应用) 表单项type="file" 表单的提交方式是post 表单的enctype属性是多部分表单形式,及enctype= ...

  8. (数据科学学习手札117)Python+Dash快速web应用开发——交互表格篇(下)

    本文示例代码已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 这是我的系列教程Python+Dash快速web ...

  9. 001 - 使用鸿蒙WebView创建简单浏览器 step 1

    打开官网,找到WebView的文档(模拟器不支持) 鸿蒙webview的开发指南(原始链接,方便大家识别并点击):https://developer.harmonyos.com/cn/docs/doc ...

  10. aws eks上部署 ingress-nginx 加NLB

    转载自https://kubernetes.github.io/ingress-nginx/deploy/#aws In AWS we use a Network load balancer (NLB ...