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 ...
随机推荐
- selenium-Python之定位下拉框选择
1.通过select 进行定位下拉框 下拉框如图所示 通过代码定位 #通过index进行选择Select(driver.find_element_by_id("cardType") ...
- 洛谷 P1311 选择客栈
题目描述 丽江河边有n 家很有特色的客栈,客栈按照其位置顺序从 1 到n 编号.每家客栈都按照某一种色调进行装饰(总共 k 种,用整数 0 ~ k-1 表示),且每家客栈都设有一家咖啡店,每家咖啡店均 ...
- Cordova应用的JavaScript代码和自定义插件代码的调试
我之前写过三篇Cordova相关的技术文章.当我们使用Cordova将自己开发的前端应用打包安装到手机上后,可能会遇到需要调试Cordova应用的时候. 本文就介绍Cordova应用的调试步骤. 如果 ...
- 校内选拔I题题解 构造题 Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) ——D
http://codeforces.com/contest/574/problem/D Bear and Blocks time limit per test 1 second memory limi ...
- AEE加密解密
from Crypto.Cipher import AESfrom binascii import b2a_hex, a2b_hex class AesHandler(object): def ...
- selenium--Xpath定位
前戏 前面介绍过了七种定位方式,今天来介绍最后一种,也是最强大,本人最常用的定位方式xpath Xpath 即为 xml 路径语言,它是一种用来确定 xml 文档中某部分位置的语言.Xpath 基于 ...
- kubernetes概念
kubernetes blog cluster cluster是计算.存储.和网络资源的集合,kubernetes利用这些资源运行各种基于容器的应用. master master是cluster的大脑 ...
- jsTree展开根节点 设置用户图标
$("#jstree").on("loaded.jstree", function (event, data) { var n = 0; var root = ...
- SSH整合JAR包详解
如果要使用连接池,添加JAR : c3p0-0.9.1.2.jar
- 洛谷 P2668 斗地主
毒瘤题目,搞了三天-- 也没什么好讲的,就是纯搜索,先搜顺子,再搜其他的,最后单张牌和对子的时候,就不要搜索了,直接枚举,不然会T飞掉多么痛的领悟-- 主要还是靠码力 #include<iost ...