Problem statement

Given a positive integer a, find the smallest positive integer b whose multiplication of each digit equals to a.

If there is no answer or the answer is not fit in 32-bit signed integer, then return 0.

Example 1
Input:

48 

Output:

68

Example 2
Input:

15

Output:

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,
    1. if a < 2, means all it`s factors are single digit number, return the value(if it is in the range),
    2. 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的更多相关文章

  1. [LeetCode] Minimum Factorization 最小因数分解

    Given a positive integer a, find the smallest positive integer b whose multiplication of each digit ...

  2. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

  3. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  4. LeetCode All in One 题目讲解汇总(转...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通 ...

  5. 【LeetCode】Recursion(共11题)

    链接:https://leetcode.com/tag/recursion/ 247 Strobogrammatic Number II (2019年2月22日,谷歌tag) 给了一个 n,给出长度为 ...

  6. 【LeetCode】数学(共106题)

    [2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...

  7. Minimum Palindromic Factorization(最少回文串分割)

    Minimum Palindromic Factorization(最少回文串分割) 以下内容大部分(可以说除了关于回文树的部分)来自论文A Subquadratic Algorithm for Mi ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. spark简单入门

    本文由cmd markdown编辑,原始链接:https://www.zybuluo.com/jewes/note/35032 RDD是什么? RDD是Spark中的抽象数据结构类型,任何数据在Spa ...

  2. python3中bytes、hex和字符串相互转换

    1.字符串转bytes a = 'abcd' a1 = bytes(a,encoding('utf-8')) 2.bytes转字符串 a = b'abcd' a1 = bytes.decode(a , ...

  3. spring-security中的csrf防御机制(跨域请求伪造)

    什么是csrf? csrf又称跨域请求伪造,攻击方通过伪造用户请求访问受信任站点.CSRF这种攻击方式在2000年已经被国外的安全人员提出,但在国内,直到06年才开始被关注,08年,国内外的多个大型社 ...

  4. WINDOWS-API:取得系统语言种类-GetOEMCP

    GetOEMCP VB声明 Declare Function GetOEMCP Lib "kernel32" Alias "GetOEMCP" () As Lo ...

  5. shell脚本,按字母出现频率降序排序。

    [root@localhost oldboy]# cat file the squid project provides a number of resources toassist users de ...

  6. (25)zabbix事件通知

    概述 我们前面花了大量时间去讲解item.trigger.event都是为发送报警做准备的,什么是事件通知呢?简单的说故障发生了,zabbix会发邮件或者短信给你,告诉你服务器的一些状况. 如果没有通 ...

  7. C#图形学习笔记

    绘图常用控件.类和结构 颜色 使用System.Drawing.Color结构表示 设置颜色的方法 调用静态函数:Color.FromArgb() public static Color FromAr ...

  8. docker系列之file基本操作

    dockerfile基础操作 Dockerfile 是记录了镜像是如何被构建出来的配置文件, 可以被 docker 直接执行以创建一个镜像. 它的样子: FROM ubuntu:14.04 MAINT ...

  9. verdi知识点

    引用:http://blog.csdn.net/naclkcl9/article/details/5425936 1. verdi 加强了active anotation, active trace和 ...

  10. 王小胖之 URL编码和解码

    使用场景:程序员使用较多,主要是图个方便,实现很简单 实现功能:URL编码 和URL解码 数据实例: 输入:王小胖好啊,王小胖顶呱呱!! ~~ english 123 !@#$%^&*()_+ ...