264. Ugly NumberII & 313. Super Ugly Number

327 查看

264 Ugly NumberII
题目:
Write a program to find the n-th ugly number.

Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers.

Note that 1 is typically treated as an ugly number.

解答:
这个问题最主要的就是如果按顺序找出ugly number, 那么我们如果能想到把以2, 3, 5为因子的这些ugly number分成三个list, 然后在每次输出时取list里最小的那个数输出就可以解决了。代码如下:

public class Solution {
    // (1) 1*2, 2*2, 3*2, 4*2,...
    // (2) 1*3, 2*3, 3*3, 4*3,...
    // (3) 1*5, 2*5, 3*5, 4*5,...
    class Num {
        int index, factor;
        public Num(int index, int factor) {
            this.index = index;
            this.factor = factor;
        }
    }

    public int nthUglyNumber(int n) {
        int[] ugly = new int[n];
        ugly[0] = 1;
        Num l2 = new Num(1, 2);
        Num l3 = new Num(1, 3);
        Num l5 = new Num(1, 5);
        for (int i = 1; i < n; i++) {
            int min = Math.min(l2.factor, Math.min(l3.factor, l5.factor));
            ugly[i] = min;
            if (l2.factor == min) {
                l2.factor = ugly[l2.index++] * 2;
            }
            if (l3.factor == min) {
                l3.factor = ugly[l3.index++] * 3;
            }
            if (l5.factor == min) {
                l5.factor = ugly[l5.index++] * 5;
            }
        }
        return ugly[n - 1];
    }
}

其实这道题还有一个更in general的解法,跟super ugly number可以用一样的解法:

public int nthUglyNumber(int n) {
    int[] primes = {2, 3, 5};
    int[] ugly = new int[n];
    int[] index = new int[primes.length];
    
    ugly[0] = 1;
    for (int i = 1; i < n; i++) {
        ugly[i] = Integer.MAX_VALUE;
        for (int j = 0; j < primes.length; j++) {
            //从每个prime现在乘到的数开始继续往下乘
            ugly[i] = Math.min(ugly[i], primes[j] * ugly[index[j]]);
        }
        
        for (int j = 0; j < primes.length; j++) {
            while (primes[j] * ugly[index[j]] == ugly[i]) {
                index[j]++;
            }
        }
    }
    return ugly[n - 1];
}

313 Super Ugly Number
题目:
Write a program to find the nth super ugly number.

Super ugly numbers are positive numbers whose all prime factors are in the given prime list primes of size k. For example, [1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32] is the sequence of the first 12 super ugly numbers given primes = [2, 7, 13, 19] of size 4.

Note:
(1) 1 is a super ugly number for any given primes.
(2) The given numbers in primes are in ascending order.
(3) 0 < k ≤ 100, 0 < n ≤ 106, 0 < primes[i] < 1000.

解答:

public int nthSuperUglyNumber(int n, int[] primes) {
    int[] ugly = new int[n];
    int[] index = new int[primes.length];
    
    ugly[0] = 1;
    for (int i = 1; i < n; i++) {
        ugly[i] = Integer.MAX_VALUE;
        //找到最小的值
        for (int j = 0; j < primes.length; j++) {
            ugly[i] = Math.min(ugly[i], primes[j] * ugly[index[j]]);
        }
        //去重
        for (int j = 0; j < primes.length; j++) {
            while (ugly[i] == primes[j] * ugly[index[j]]) {
                index[j]++;
            }
        }
    }
    return ugly[n - 1];
}