GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • LeetCodeJavascript.com
  • Star on GitHub

This view

  • Original page
  • New address

GeezerVison is showing leetcodejavascript.com — #625 - Minimum Factorization - LeetCode JavaScript Solutions

Back to all solutions

#625 - Minimum Factorization

Medium • View on LeetCode • View on GitHub
Math Greedy

Problem Description

Given a positive integer num, return the smallest positive integer x whose multiplication of each digit equals num. If there is no answer or the answer is not fit in 32-bit signed integer, return 0.

Solution

/**
 * @param {number} num
 * @return {number}
 */
var smallestFactorization = function(num) {
  if (num < 2) return num;

  const digits = [];
  for (let divisor = 9; divisor > 1; divisor--) {
    while (num % divisor === 0) {
      digits.push(divisor);
      num /= divisor;
    }
  }

  if (num > 1) return 0;

  let result = 0;
  for (const digit of digits.reverse()) {
    result = result * 10 + digit;
    if (result > 2**31 - 1) return 0;
  }

  return result;
};
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.