625. Minimum Factorization
Problem statement
Given a positive integer
a
, find the smallest positive integerb
whose multiplication of each digit equals toa
.If there is no answer or the answer is not fit in 32-bit signed integer, then return 0.
Example 1
Input:48Output:
68Example 2
Input:15Output:
35
Solution
This is the third problem for weekly contest 37. Initially, I thought it is a DFS problem. Actually, it is much easier. Although it is tagged with recursion in leetcode, I prefer it is a pure math.
Since the input number should only contains the factors which are less than 10. We loop from 9 to 2, to divide the input number, until it is not divisible by any number in [2, 9].
Suppose the input number is a, general idea is as following:
- Loop i from 9 to 2.
- In each loop, if a is divisible by i, update the min factorization value and a. Goes to next loop until a is not divisible by i
- After exiting the loop,
- if a < 2, means all it`s factors are single digit number, return the value(if it is in the range),
- Otherwise, it means there is at least one factor, that is greater than 9, return 0.
Time complexity is O(8loga), space complexity is O(1).
class Solution {
public:
int smallestFactorization(int a) {
if(a < ){
return a;
}
long min_factorization = ;
long mul = ;
for(int i = ; i >= ; i--){
while(a % i == ){
min_factorization += mul * i;
mul *= ;
a /= i;
}
}
return (a < && min_factorization < INT_MAX) ? min_factorization : ;
}
};
625. Minimum Factorization的更多相关文章
- [LeetCode] Minimum Factorization 最小因数分解
Given a positive integer a, find the smallest positive integer b whose multiplication of each digit ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- LeetCode All in One 题目讲解汇总(转...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通 ...
- 【LeetCode】Recursion(共11题)
链接:https://leetcode.com/tag/recursion/ 247 Strobogrammatic Number II (2019年2月22日,谷歌tag) 给了一个 n,给出长度为 ...
- 【LeetCode】数学(共106题)
[2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...
- Minimum Palindromic Factorization(最少回文串分割)
Minimum Palindromic Factorization(最少回文串分割) 以下内容大部分(可以说除了关于回文树的部分)来自论文A Subquadratic Algorithm for Mi ...
- [LeetCode] Minimum Moves to Equal Array Elements II 最少移动次数使数组元素相等之二
Given a non-empty integer array, find the minimum number of moves required to make all array element ...
- [LeetCode] Minimum Moves to Equal Array Elements 最少移动次数使数组元素相等
Given a non-empty integer array of size n, find the minimum number of moves required to make all arr ...
随机推荐
- 大数据freestyle: 共享单车轨迹数据助力城市合理规划自行车道
编者按:近年来,异军突起的共享单车极大地解决了人们共同面临的“最后一公里”难题,然而,共享单车发展迅猛,自行车道建设却始终没有能够跟上脚步.幸运的是摩拜单车大量的轨迹数据为我们提供了一种新的思路:利用 ...
- COGS 930. [河南省队2012] 找第k小的数
题目描述 看到很短的题目会让人心情愉悦,所以给出一个长度为N的序列A1,A2,A3,...,AN, 现在有M个询问,每个询问都是Ai...Aj中第k小的数等于多少. 输入格式 第一行两个正整数N,M. ...
- SQLite-表达式
SQLite -表达式 一个表达式是一个或多个值的组合,运算符和SQL函数,评价一个值. SQL表达式就像公式和都写在查询语言.您还可以使用为特定的数据集查询数据库. 语法: 考虑到SELECT语句的 ...
- 导致实例逐出的五大问题 (文档 ID 1526186.1)
适用于: Oracle Database - Enterprise Edition - 版本 10.2.0.1 到 11.2.0.3 [发行版 10.2 到 11.2]本文档所含信息适用于所有平台 用 ...
- 关于用终端运行php来测试推送的问题
照网上的方法,合并好了证书的pem,密码也是对的,然后也写好了推送用的php文件,在终端里php这个文件,报错报错内容是:Warning: stream_socket_client(): SSL op ...
- Ukulele 调音
正常的持琴姿势时,从上到下依次是:4,3,2,1弦,音从上往下是:G,C,E,A: 3弦 - C - Do - D - Re 2弦 - E - Mi - F - Fa 4弦 - G -So 1弦 - ...
- 使用iptables缓解DDOS及CC攻击
使用iptables缓解DDOS及CC攻击 LINUX 追马 7个月前 (02-09) 465浏览 0评论 缓解DDOS攻击 防止SYN攻击,轻量级预防 iptables -N syn-flo ...
- GIMP用Path作画了解一下
先准备好Path的底稿,只是实验学到的东西,粗糙了点.Paint through the Path,顾名思义,就是沿着Path作画: 1/如果选择的是Stroke line,可以根据自己的喜好,调节S ...
- Python_编程题集_001_词法解析
1.词法解析: 我的是名字是ths,今年18岁 语法分析后得到结果如下: 数字:18 中文:我的名字是 今年 岁 拼音:ths 符号:,. 请编写程序实现该词法分析功能 string模块解: impo ...
- java各种数据库连接
MySQL: String Driver="com.mysql.jdbc.Driver"; //驱动程序 String URL="jdbc:mys ...