Smallest Divisible Digit Product II Problem
Description
LeetCode Problem 3348.
You are given a string num which represents a positive integer, and an integer t.
A number is called zero-free if none of its digits are 0.
Return a string representing the smallest zero-free number greater than or equal to num such that the product of its digits is divisible by t. If no such number exists, return "-1".
Example 1:
Input: num = "1234", t = 256
Output: "1488"
Explanation:
The smallest zero-free number that is greater than 1234 and has the product of its digits divisible by 256 is 1488, with the product of its digits equal to 256.
Example 2:
Input: num = "12355", t = 50
Output: "12355"
Explanation:
12355 is already zero-free and has the product of its digits divisible by 50, with the product of its digits equal to 150.
Example 3:
Input: num = "11111", t = 26
Output: "-1"
Explanation:
No number greater than 11111 has the product of its digits divisible by 26.
Constraints:
2 <= num.length <= 2 * 10^5numconsists only of digits in the range['0', '9'].numdoes not contain leading zeros.1 <= t <= 10^14
Solution
Approach: Greedy + Factorization
Only the primes 2, 3, 5, 7 can appear in a digit product, so if t has any other prime factor there is no answer. Otherwise, compute the smallest multiset of digits whose product is divisible by t: pack the factors of 2, 3, 5, 7 into the digits 8, 9, 6, 4 and the rest as singles, which keeps the length minimal.
If num itself (with no zero digit) already has a product divisible by t, return it. Otherwise scan the prefix of num from the right: for each position, try raising that digit to a larger nonzero digit, and check that the remaining suffix can be filled with ones plus the digits still needed. The first (rightmost) position that works, with the smallest digit that works, gives the answer. If no position works, the answer is longer than num: prepend ones to the minimal digit multiset, sorted ascending.
Complexity: O(n * 10 + n) after a prefix-product precomputation, O(n) space, where n is the length of num.
class Solution:
def smallestNumber(self, num: str, t: int) -> str:
digit_primes = [
[0, 0, 0, 0],
[0, 0, 0, 0],
[1, 0, 0, 0],
[0, 1, 0, 0],
[2, 0, 0, 0],
[0, 0, 1, 0],
[1, 1, 0, 0],
[0, 0, 0, 1],
[3, 0, 0, 0],
[0, 2, 0, 0],
]
def factorize(target: int):
counts = [0, 0, 0, 0]
for i, p in enumerate((2, 3, 5, 7)):
while target % p == 0:
target //= p
counts[i] += 1
return counts, target == 1
def subtract(a, b):
return [max(0, x - y) for x, y in zip(a, b)]
def to_digits(primes):
count8 = primes[0] // 3
remaining2 = primes[0] % 3
count9 = primes[1] // 2
count3 = primes[1] % 2
count4 = remaining2 // 2
count2 = remaining2 % 2
count6 = 0
if count2 == 1 and count3 == 1:
count2 = count3 = 0
count6 = 1
if count3 == 1 and count4 == 1:
count2 = 1
count6 = 1
count3 = count4 = 0
return [
0,
0,
count2,
count3,
count4,
primes[2],
count6,
primes[3],
count8,
count9,
]
def construct(digits) -> str:
return "".join(str(d) * digits[d] for d in range(2, 10))
required, ok = factorize(t)
if not ok:
return "-1"
need = to_digits(required)
if sum(need) > len(num):
return construct(need)
prefix = [0, 0, 0, 0]
for ch in num:
d = ord(ch) - 48
for i in range(4):
prefix[i] += digit_primes[d][i]
first_zero = num.find("0")
if first_zero == -1:
first_zero = len(num)
if all(r <= p for r, p in zip(required, prefix)):
return num
n = len(num)
for i in range(n - 1, -1, -1):
d = ord(num[i]) - 48
prefix = subtract(prefix, digit_primes[d])
space = n - 1 - i
if i > first_zero:
continue
for bigger in range(d + 1, 10):
suffix = to_digits(
subtract(subtract(required, prefix), digit_primes[bigger])
)
if sum(suffix) <= space:
return (
num[:i]
+ str(bigger)
+ "1" * (space - sum(suffix))
+ construct(suffix)
)
ext = to_digits(required)
return "1" * (n + 1 - sum(ext)) + construct(ext)