Skip to content

Commit 28664d9

Browse files
authored
feat: add solutions to lc problem: No.2847 (#1583)
No.2847.Smallest Number With Given Digit Product
1 parent cfe3e3c commit 28664d9

File tree

10 files changed

+393
-0
lines changed

10 files changed

+393
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
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>&quot;-1&quot;</code><em> if no such number exists</em>.</p>
10+
11+
<p>&nbsp;</p>
12+
<p><strong class="example">Example 1:</strong></p>
13+
14+
<pre>
15+
<strong>Input:</strong> n = 105
16+
<strong>Output:</strong> &quot;357&quot;
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 &quot;105&quot;.
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> &quot;7&quot;
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 &quot;7&quot; 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> &quot;-1&quot;
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 &quot;-1&quot;.
34+
</pre>
35+
36+
<p>&nbsp;</p>
37+
<p><strong>Constraints:</strong></p>
38+
39+
<ul>
40+
<li><code>1 &lt;= n &lt;= 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 -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
# [2847. Smallest Number With Given Digit Product](https://leetcode.com/problems/smallest-number-with-given-digit-product)
2+
3+
[中文文档](/solution/2800-2899/2847.Smallest%20Number%20With%20Given%20Digit%20Product/README.md)
4+
5+
## Description
6+
7+
<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>&quot;-1&quot;</code><em> if no such number exists</em>.</p>
8+
9+
<p>&nbsp;</p>
10+
<p><strong class="example">Example 1:</strong></p>
11+
12+
<pre>
13+
<strong>Input:</strong> n = 105
14+
<strong>Output:</strong> &quot;357&quot;
15+
<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 &quot;105&quot;.
16+
</pre>
17+
18+
<p><strong class="example">Example 2:</strong></p>
19+
20+
<pre>
21+
<strong>Input:</strong> n = 7
22+
<strong>Output:</strong> &quot;7&quot;
23+
<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 &quot;7&quot; would be the answer.
24+
</pre>
25+
26+
<p><strong class="example">Example 3:</strong></p>
27+
28+
<pre>
29+
<strong>Input:</strong> n = 44
30+
<strong>Output:</strong> &quot;-1&quot;
31+
<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 &quot;-1&quot;.
32+
</pre>
33+
34+
<p>&nbsp;</p>
35+
<p><strong>Constraints:</strong></p>
36+
37+
<ul>
38+
<li><code>1 &lt;= n &lt;= 10<sup>18</sup></code></li>
39+
</ul>
40+
41+
## Solutions
42+
43+
<!-- tabs:start -->
44+
45+
### **Python3**
46+
47+
```python
48+
class Solution:
49+
def smallestNumber(self, n: int) -> str:
50+
cnt = [0] * 10
51+
for i in range(9, 1, -1):
52+
while n % i == 0:
53+
n //= i
54+
cnt[i] += 1
55+
if n > 1:
56+
return "-1"
57+
ans = "".join(str(i) * cnt[i] for i in range(2, 10))
58+
return ans if ans else "1"
59+
```
60+
61+
### **Java**
62+
63+
```java
64+
class Solution {
65+
public String smallestNumber(long n) {
66+
int[] cnt = new int[10];
67+
for (int i = 9; i > 1; --i) {
68+
while (n % i == 0) {
69+
++cnt[i];
70+
n /= i;
71+
}
72+
}
73+
if (n > 1) {
74+
return "-1";
75+
}
76+
StringBuilder sb = new StringBuilder();
77+
for (int i = 2; i < 10; ++i) {
78+
while (cnt[i] > 0) {
79+
sb.append(i);
80+
--cnt[i];
81+
}
82+
}
83+
String ans = sb.toString();
84+
return ans.isEmpty() ? "1" : ans;
85+
}
86+
}
87+
```
88+
89+
### **C++**
90+
91+
```cpp
92+
class Solution {
93+
public:
94+
string smallestNumber(long long n) {
95+
int cnt[10]{};
96+
for (int i = 9; i > 1; --i) {
97+
while (n % i == 0) {
98+
n /= i;
99+
++cnt[i];
100+
}
101+
}
102+
if (n > 1) {
103+
return "-1";
104+
}
105+
string ans;
106+
for (int i = 2; i < 10; ++i) {
107+
ans += string(cnt[i], '0' + i);
108+
}
109+
return ans == "" ? "1" : ans;
110+
}
111+
};
112+
```
113+
114+
### **Go**
115+
116+
```go
117+
func smallestNumber(n int64) string {
118+
cnt := [10]int{}
119+
for i := 9; i > 1; i-- {
120+
for n%int64(i) == 0 {
121+
cnt[i]++
122+
n /= int64(i)
123+
}
124+
}
125+
if n != 1 {
126+
return "-1"
127+
}
128+
sb := &strings.Builder{}
129+
for i := 2; i < 10; i++ {
130+
for j := 0; j < cnt[i]; j++ {
131+
sb.WriteByte(byte(i) + '0')
132+
}
133+
}
134+
ans := sb.String()
135+
if len(ans) > 0 {
136+
return ans
137+
}
138+
return "1"
139+
}
140+
```
141+
142+
### **...**
143+
144+
```
145+
146+
```
147+
148+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
string smallestNumber(long long n) {
4+
int cnt[10]{};
5+
for (int i = 9; i > 1; --i) {
6+
while (n % i == 0) {
7+
n /= i;
8+
++cnt[i];
9+
}
10+
}
11+
if (n > 1) {
12+
return "-1";
13+
}
14+
string ans;
15+
for (int i = 2; i < 10; ++i) {
16+
ans += string(cnt[i], '0' + i);
17+
}
18+
return ans == "" ? "1" : ans;
19+
}
20+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
func smallestNumber(n int64) string {
2+
cnt := [10]int{}
3+
for i := 9; i > 1; i-- {
4+
for n%int64(i) == 0 {
5+
cnt[i]++
6+
n /= int64(i)
7+
}
8+
}
9+
if n != 1 {
10+
return "-1"
11+
}
12+
sb := &strings.Builder{}
13+
for i := 2; i < 10; i++ {
14+
for j := 0; j < cnt[i]; j++ {
15+
sb.WriteByte(byte(i) + '0')
16+
}
17+
}
18+
ans := sb.String()
19+
if len(ans) > 0 {
20+
return ans
21+
}
22+
return "1"
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public String smallestNumber(long n) {
3+
int[] cnt = new int[10];
4+
for (int i = 9; i > 1; --i) {
5+
while (n % i == 0) {
6+
++cnt[i];
7+
n /= i;
8+
}
9+
}
10+
if (n > 1) {
11+
return "-1";
12+
}
13+
StringBuilder sb = new StringBuilder();
14+
for (int i = 2; i < 10; ++i) {
15+
while (cnt[i] > 0) {
16+
sb.append(i);
17+
--cnt[i];
18+
}
19+
}
20+
String ans = sb.toString();
21+
return ans.isEmpty() ? "1" : ans;
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def smallestNumber(self, n: int) -> str:
3+
cnt = [0] * 10
4+
for i in range(9, 1, -1):
5+
while n % i == 0:
6+
n //= i
7+
cnt[i] += 1
8+
if n > 1:
9+
return "-1"
10+
ans = "".join(str(i) * cnt[i] for i in range(2, 10))
11+
return ans if ans else "1"

0 commit comments

Comments
 (0)