|
| 1 | +# [2847. Smallest Number With Given Digit Product](https://leetcode.cn/problems/smallest-number-with-given-digit-product) |
| 2 | + |
| 3 | +[English Version](/solution/2800-2899/2847.Smallest%20Number%20With%20Given%20Digit%20Product/README_EN.md) |
| 4 | + |
| 5 | +## 题目描述 |
| 6 | + |
| 7 | +<!-- 这里写题目描述 --> |
| 8 | + |
| 9 | +<p>Given a <strong>positive</strong> integer <code>n</code>, return <em>a string representing the <strong>smallest positive</strong> integer such that the product of its digits is equal to</em> <code>n</code><em>, or </em><code>"-1"</code><em> if no such number exists</em>.</p> |
| 10 | + |
| 11 | +<p> </p> |
| 12 | +<p><strong class="example">Example 1:</strong></p> |
| 13 | + |
| 14 | +<pre> |
| 15 | +<strong>Input:</strong> n = 105 |
| 16 | +<strong>Output:</strong> "357" |
| 17 | +<strong>Explanation:</strong> 3 * 5 * 7 = 105. It can be shown that 357 is the smallest number with a product of digits equal to 105. So the answer would be "105". |
| 18 | +</pre> |
| 19 | + |
| 20 | +<p><strong class="example">Example 2:</strong></p> |
| 21 | + |
| 22 | +<pre> |
| 23 | +<strong>Input:</strong> n = 7 |
| 24 | +<strong>Output:</strong> "7" |
| 25 | +<strong>Explanation:</strong> Since 7 has only one digit, its product of digits would be 7. We will show that 7 is the smallest number with a product of digits equal to 7. Since the product of numbers 1 to 6 is 1 to 6 respectively, so "7" would be the answer. |
| 26 | +</pre> |
| 27 | + |
| 28 | +<p><strong class="example">Example 3:</strong></p> |
| 29 | + |
| 30 | +<pre> |
| 31 | +<strong>Input:</strong> n = 44 |
| 32 | +<strong>Output:</strong> "-1" |
| 33 | +<strong>Explanation:</strong> It can be shown that there is no number such that its product of digits is equal to 44. So the answer would be "-1". |
| 34 | +</pre> |
| 35 | + |
| 36 | +<p> </p> |
| 37 | +<p><strong>Constraints:</strong></p> |
| 38 | + |
| 39 | +<ul> |
| 40 | + <li><code>1 <= n <= 10<sup>18</sup></code></li> |
| 41 | +</ul> |
| 42 | + |
| 43 | +## 解法 |
| 44 | + |
| 45 | +<!-- 这里可写通用的实现逻辑 --> |
| 46 | + |
| 47 | +**方法一:质因数分解 + 贪心** |
| 48 | + |
| 49 | +我们考虑对数字 $n$ 进行质因数分解,如果 $n$ 的质因数中存在大于 $9$ 的质数,那么一定无法找到符合条件的数字,因为大于 $9$ 的质数无法通过 $1$ 到 $9$ 的数字相乘得到,例如 $11$ 无法通过 $1$ 到 $9$ 的数字相乘得到,因此我们只需要考虑 $n$ 的质因数中是否存在大于 $9$ 的质数即可,如果存在,直接返回 $-1$。 |
| 50 | + |
| 51 | +否则,如果质因数中包含 $7$ 和 $5$,那么数字 $n$ 首先可以拆分为若干个 $7$ 和 $5$,两个数字 $3$ 可以合成一个数字 $9$,三个数字 $2$ 可以合成一个数字 $8$,数字 $2$ 和数字 $3$ 可以合成一个数字 $6$,因此我们只需要将数字拆分为 $[2,..9]$ 的数字即可,我们可以使用贪心的方法,优先拆分出数字 $9$,然后拆分出数字 $8$,依次类推。 |
| 52 | + |
| 53 | +时间复杂度 $O(\log n)$,空间复杂度 $O(1)$。 |
| 54 | + |
| 55 | +<!-- tabs:start --> |
| 56 | + |
| 57 | +### **Python3** |
| 58 | + |
| 59 | +<!-- 这里可写当前语言的特殊实现逻辑 --> |
| 60 | + |
| 61 | +```python |
| 62 | +class Solution: |
| 63 | + def smallestNumber(self, n: int) -> str: |
| 64 | + cnt = [0] * 10 |
| 65 | + for i in range(9, 1, -1): |
| 66 | + while n % i == 0: |
| 67 | + n //= i |
| 68 | + cnt[i] += 1 |
| 69 | + if n > 1: |
| 70 | + return "-1" |
| 71 | + ans = "".join(str(i) * cnt[i] for i in range(2, 10)) |
| 72 | + return ans if ans else "1" |
| 73 | +``` |
| 74 | + |
| 75 | +### **Java** |
| 76 | + |
| 77 | +<!-- 这里可写当前语言的特殊实现逻辑 --> |
| 78 | + |
| 79 | +```java |
| 80 | +class Solution { |
| 81 | + public String smallestNumber(long n) { |
| 82 | + int[] cnt = new int[10]; |
| 83 | + for (int i = 9; i > 1; --i) { |
| 84 | + while (n % i == 0) { |
| 85 | + ++cnt[i]; |
| 86 | + n /= i; |
| 87 | + } |
| 88 | + } |
| 89 | + if (n > 1) { |
| 90 | + return "-1"; |
| 91 | + } |
| 92 | + StringBuilder sb = new StringBuilder(); |
| 93 | + for (int i = 2; i < 10; ++i) { |
| 94 | + while (cnt[i] > 0) { |
| 95 | + sb.append(i); |
| 96 | + --cnt[i]; |
| 97 | + } |
| 98 | + } |
| 99 | + String ans = sb.toString(); |
| 100 | + return ans.isEmpty() ? "1" : ans; |
| 101 | + } |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +### **C++** |
| 106 | + |
| 107 | +```cpp |
| 108 | +class Solution { |
| 109 | +public: |
| 110 | + string smallestNumber(long long n) { |
| 111 | + int cnt[10]{}; |
| 112 | + for (int i = 9; i > 1; --i) { |
| 113 | + while (n % i == 0) { |
| 114 | + n /= i; |
| 115 | + ++cnt[i]; |
| 116 | + } |
| 117 | + } |
| 118 | + if (n > 1) { |
| 119 | + return "-1"; |
| 120 | + } |
| 121 | + string ans; |
| 122 | + for (int i = 2; i < 10; ++i) { |
| 123 | + ans += string(cnt[i], '0' + i); |
| 124 | + } |
| 125 | + return ans == "" ? "1" : ans; |
| 126 | + } |
| 127 | +}; |
| 128 | +``` |
| 129 | +
|
| 130 | +### **Go** |
| 131 | +
|
| 132 | +```go |
| 133 | +func smallestNumber(n int64) string { |
| 134 | + cnt := [10]int{} |
| 135 | + for i := 9; i > 1; i-- { |
| 136 | + for n%int64(i) == 0 { |
| 137 | + cnt[i]++ |
| 138 | + n /= int64(i) |
| 139 | + } |
| 140 | + } |
| 141 | + if n != 1 { |
| 142 | + return "-1" |
| 143 | + } |
| 144 | + sb := &strings.Builder{} |
| 145 | + for i := 2; i < 10; i++ { |
| 146 | + for j := 0; j < cnt[i]; j++ { |
| 147 | + sb.WriteByte(byte(i) + '0') |
| 148 | + } |
| 149 | + } |
| 150 | + ans := sb.String() |
| 151 | + if len(ans) > 0 { |
| 152 | + return ans |
| 153 | + } |
| 154 | + return "1" |
| 155 | +} |
| 156 | +``` |
| 157 | + |
| 158 | +### **...** |
| 159 | + |
| 160 | +``` |
| 161 | +
|
| 162 | +``` |
| 163 | + |
| 164 | +<!-- tabs:end --> |
0 commit comments