Skip to content

Commit ee87df2

Browse files
authored
Create smallest-number-with-given-digit-product.cpp
1 parent 65282b1 commit ee87df2

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Time: O(logn)
2+
// Space: O(logn)
3+
4+
// greedy
5+
class Solution {
6+
public:
7+
string smallestNumber(long long n) {
8+
string result;
9+
for (int d = 9; d >= 2; --d) {
10+
for (; n % d == 0; n /= d) {
11+
result.push_back('0' + d);
12+
}
13+
}
14+
reverse(begin(result), end(result));
15+
if (empty(result)) {
16+
result = "1";
17+
}
18+
return n == 1 ? result : "-1";
19+
}
20+
};

0 commit comments

Comments
 (0)