Java solution beats 100%


  • 0

    Try to decompose a into product of single digit numbers, and if we can use larger one don't use multiple smaller ones. e.g if we can use 8 don't use 2 and 4 instead. Then we construct the number by putting smaller digits at MSB

        public int smallestFactorization(int a) {
            if (a < 10) return a;
            int[] count = new int[10];
            for (int i = 9; i > 1; i--) {
                while (a % i == 0) {
                    count[i]++;
                    a /= i;
                }
            }
            if (a != 1) return 0;
            long ans = 0;
            for (int i = 2; i <= 9; i++) {
                while (count[i]-- > 0) {
                    ans = ans*10+i;
                    if (ans > Integer.MAX_VALUE) return 0;
                }
            }
            return (int) ans;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.