625. Minimum Factorization
Problem statement
Given a positive integer
a, find the smallest positive integerbwhose 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 ...
随机推荐
- ZOJ 3627 Treasure Hunt II (贪心,模拟)
题意:有n个城市并排着,每个城市有些珠宝,有两个人站在第s个城市准备收集珠宝,两人可以各自行动,但两人之间的距离不能超过dis,而且每经过一个城市就需要消耗1天,他们仅有t天时间收集珠宝,问最多能收集 ...
- asp.net mvc集成log4net
第一步:在web项目的引用中添加log4net.dll,可以通过Nuget直接下载并安装: 第二步:在web项目的web.config配置文件的configuration节点内添加log4net节点, ...
- JavaWeb项目实现图片验证码
一.什么是图片验证码? 可以参考下面这张图: 我们在一些网站注册的时候,经常需要填写以上图片的信息. 这种图片验证方式是我们最常见的形式,它可以有效的防范恶意攻击者采用恶意工具,调用“动态验证码短信获 ...
- 快学UiAutomator各种框架介绍
Monkey 编写语言:命令行 运行环境:使用adb连接PC运行测试对象:Android平台自动化测试的一种手段,通过Monkey程序模拟用户触摸屏幕.滑动Trackball.按键等操作来对设备上的程 ...
- 自己开发一个APP需要多少钱
广州APP开发公司[启汇网络]经常遇到有开发定制APP软件需求的企业,通常第一句问的就是“开发一款APP需要多少钱”,在做完客户行业的市场调查后,再了解客... 广州APP开发公司[启汇网络]经常遇到 ...
- ios retain copy 以及copy协议
阅读本文之前首先了解Copy与Retain的区别: Copy是创建一个新对象,Retain是创建一个指针,引用对象计数加1. Copy属性表示两个对象内容相同,新的对象retain为1 ,与旧有对象的 ...
- (47)zabbix报警媒介:Ez Texting
Ez Texting是zabbix的技术合作伙伴,主要提供短信服务,用手机注册账号,便可以使用它来发送短信了,不过他只支持美国和加拿大的手机号码,并且应该是收费的.没有美国/加拿大手机号码的朋友请绕行 ...
- Java测试技巧
快捷键 修改快捷键 window->preference->general->keys: 查找引用:ctrl+shift+g 覆盖测试:alt+shift+E,T 复制一行:ctrl ...
- link与@import导入css样式区别
XML/HTML代码<link rel="stylesheet" rev="stylesheet" href="CSS文件" type ...
- TB平台搭建之三
有简单到复杂,可以简单的决不复杂化,事情从可控开始,即使再好的技术如果不可控最好不要用否则以后的debug可能比较麻烦. 无论是搭建平台还是写复杂的case都是尽量从简单开始,不要上来复杂,否则deb ...