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 ...
随机推荐
- 【Web应用-Web作业】Web 作业无法直接运行 jar 文件
问题描述 在经典管理门户中将直接压缩的 jar 文件打包为 zip 包,上传到 web 作业时报错. 解决方法 jar 文件的运行需要依托于 java 进程,所以在运行 jar 文件时,我们都会以格式 ...
- 按Home键切换到后台后会触发libGPUSupportMercury.dylib: gpus_ReturnNotPermittedKillClient导致crash
转自:http://www.eoeandroid.com/thread-251598-1-1.html 好像有很多朋友都碰到过这个问题,即在真机调试时,按hone键返回桌面,再回到app时,app会c ...
- Web项目之Django基础
Django目录: python项目Django(web服务) python项目Django(HTTP协议) python项目Django(Django的安装与使用) python项目Django(U ...
- 1991: C语言实验——大小写转换
1991: C语言实验——大小写转换 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 183 Solved: 109[Submit][Status][We ...
- 爬虫4_python2
import urllib2 response = urllib2.urlopen("https://www.baidu.com") print response.read() 构 ...
- Xcode及Mac快捷键
1. 文件 CMD + N: 新文件CMD + SHIFT + N: 新项目CMD + O: 打开CMD + S: 保存CMD + SHIFT + S: 另存为CMD + W: 关闭窗口CMD + S ...
- Linux 中 MySQL 授权远程连接
说明:当别的机子(IP )通过客户端的方式在没有授权的情况下是无法连接 MySQL 数据库的,如果需要远程连接 Linux 系统上的 MySQL 时,必须为其 IP 和具体用户进行授权.一般 root ...
- web前端常用的封装方法
1.放大镜 //页面加载完毕后执行 window.onload = function () { var oDemo = document.getElementById('demo'); var oMa ...
- perl学习之:shift/unshift
perl中shift 和unshift 操作 2008-02-02 11:18:04| 分类: Perl语言|举报|字号 订阅 ############################### ...
- 牛客网暑期ACM多校训练营(第一场)J Different Integers(树状数组, 离线)
题意: 给定n个数字, 然后给出m个区间, 求区间外其他数字的种类有多少. 分析: 将区间以r为基准升序排序, 每次处理pre~r的数字第一次出现的位置. #include<bits/stdc+ ...