56. digitsProduct


Task

Given an integer product, find the smallest positive (i.e. greater than 0) integer the product of whose digits is equal to product. If there is no such integer, return -1 instead.

Example

  • For product = 12, the output should be
    digitsProduct(product) = 26;
  • For product = 19, the output should be
    digitsProduct(product) = -1.

Input/Output

  • [execution time limit]
    4 seconds (py3)

  • [input] integer product
    Guaranteed constraints: 0 ≤ product ≤ 600.

  • [output] integer

My Solution

def digitsProduct(product):
    if product == 0:
        return 10
    divisor = []
    while product > 9:
        for i in reversed(range(2, 10)):
            if product % i == 0:
                divisor.append(i)
                product = product / i
                break
            if i == 2:
                return -1
    divisor.append(int(product))
    return int(''.join(map(str, divisor[::-1])))