KatInterview

264. Ugly Number II

MediumSolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

Method: Dynamic Programming with Three Pointers

This problem asks for the n-th ugly number. A direct approach of checking every integer for its ugliness would be too slow because ugly numbers are sparse. A more efficient method is to generate only the ugly numbers in sequence.

Solution

Ugly Number II in Python

class Solution:
    def nthUglyNumber(self, n: int) -> int:
        # DP array to store the ugly numbers.
        dp = [0] * n
        # The first ugly number is 1.
        dp[0] = 1

        # Pointers for the three sequences of ugly numbers multiplied by 2, 3, and 5.
        p2, p3, p5 = 0, 0, 0

        # We have already found one ugly number, so we start the loop from 1.
        for i in range(1, n):
            # Candidates for the next ugly number.
            next2 = dp[p2] * 2
            next3 = dp[p3] * 3
            next5 = dp[p5] * 5

            # The next ugly number is the minimum of the candidates.
            dp[i] = min(next2, next3, next5)

            # Advance the pointer(s) that generated the new ugly number.
            # We use separate 'if's because a number might be generated in multiple ways,
            # e.g., 6 = 2 * 3 = 3 * 2. In such cases, both pointers must advance.
            if dp[i] == next2:
                p2 += 1
            if dp[i] == next3:
                p3 += 1
            if dp[i] == next5:
                p5 += 1
        
        # The n-th ugly number is at index n-1.
        return dp[n - 1]

Ugly Number II in Java

class Solution {
    public int nthUglyNumber(int n) {
        // DP array to store the ugly numbers.
        int[] dp = new int[n];
        // The first ugly number is 1.
        dp[0] = 1;

        // Pointers for the three sequences of ugly numbers multiplied by 2, 3, and 5.
        int p2 = 0, p3 = 0, p5 = 0;

        // We have already found one ugly number, so we start the loop from 1.
        for (int i = 1; i < n; i++) {
            // Candidates for the next ugly number.
            int next2 = dp[p2] * 2;
            int next3 = dp[p3] * 3;
            int next5 = dp[p5] * 5;

            // The next ugly number is the minimum of the candidates.
            dp[i] = Math.min(next2, Math.min(next3, next5));

            // Advance the pointer(s) that generated the new ugly number.
            // We use separate 'if's because a number might be generated in multiple ways,
            // e.g., 6 = 2 * 3 = 3 * 2. In such cases, both pointers must advance.
            if (dp[i] == next2) {
                p2++;
            }
            if (dp[i] == next3) {
                p3++;
            }
            if (dp[i] == next5) {
                p5++;
            }
        }

        // The n-th ugly number is at index n-1.
        return dp[n - 1];
    }
}

Ugly Number II in C++

class Solution {
public:
    int nthUglyNumber(int n) {
        // DP array to store the ugly numbers.
        vector<int> dp(n);
        // The first ugly number is 1.
        dp[0] = 1;

        // Pointers for the three sequences of ugly numbers multiplied by 2, 3, and 5.
        int p2 = 0, p3 = 0, p5 = 0;

        // We have already found one ugly number, so we start the loop from 1.
        for (int i = 1; i < n; ++i) {
            // Candidates for the next ugly number.
            int next2 = dp[p2] * 2;
            int next3 = dp[p3] * 3;
            int next5 = dp[p5] * 5;

            // The next ugly number is the minimum of the candidates.
            dp[i] = min({next2, next3, next5});

            // Advance the pointer(s) that generated the new ugly number.
            // We use separate 'if's because a number might be generated in multiple ways,
            // e.g., 6 = 2 * 3 = 3 * 2. In such cases, both pointers must advance.
            if (dp[i] == next2) {
                p2++;
            }
            if (dp[i] == next3) {
                p3++;
            }
            if (dp[i] == next5) {
                p5++;
            }
        }

        // The n-th ugly number is at index n-1.
        return dp[n - 1];
    }
};

Ugly Number II in TypeScript

function nthUglyNumber(n: number): number {
    // DP array to store the ugly numbers.
    const dp: number[] = new Array(n);
    // The first ugly number is 1.
    dp[0] = 1;

    // Pointers for the three sequences of ugly numbers multiplied by 2, 3, and 5.
    let p2 = 0, p3 = 0, p5 = 0;

    // We have already found one ugly number, so we start the loop from 1.
    for (let i = 1; i < n; i++) {
        // Candidates for the next ugly number.
        const next2 = dp[p2] * 2;
        const next3 = dp[p3] * 3;
        const next5 = dp[p5] * 5;

        // The next ugly number is the minimum of the candidates.
        dp[i] = Math.min(next2, next3, next5);

        // Advance the pointer(s) that generated the new ugly number.
        // We use separate 'if's because a number might be generated in multiple ways,
        // e.g., 6 = 2 * 3 = 3 * 2. In such cases, both pointers must advance.
        if (dp[i] === next2) {
            p2++;
        }
        if (dp[i] === next3) {
            p3++;
        }
        if (dp[i] === next5) {
            p5++;
        }
    }

    // The n-th ugly number is at index n-1.
    return dp[n - 1];
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.