Advertisement

Ugly Number II - LeetCode 264 Solution

Ugly Number II - Complete Solution Guide

Ugly Number II is LeetCode problem 264, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

An ugly number is a positive integer whose prime factors are limited to 2 , 3 , and 5 . Given an integer n , return the n th ugly number . Example 1: Input: n = 10 Output: 12 Explanation: [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers. Example 2: Input: n = 1 Output: 1 Explanation: 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5. Constraints: 1 <= n <= 1690

Detailed Explanation

The problem asks us to find the n-th ugly number. An ugly number is a positive integer whose prime factors are limited to 2, 3, and 5. For example, the first 10 ugly numbers are [1, 2, 3, 4, 5, 6, 8, 9, 10, 12]. The input is an integer 'n' representing the position of the ugly number we want to find (1-indexed). The output is the n-th ugly number.

Solution Approach

The solution uses dynamic programming to generate the sequence of ugly numbers. It maintains an array `ugly` to store the ugly numbers in ascending order. The algorithm iteratively calculates the next ugly number by finding the minimum of the products of the existing ugly numbers with 2, 3, and 5. Three pointers, `p2`, `p3`, and `p5`, are used to track the indices of the ugly numbers that will be multiplied by 2, 3, and 5, respectively. After finding the minimum (which is the next ugly number), the corresponding pointer(s) are incremented. If a number can be generated in multiple ways, each corresponding pointer is incremented to avoid duplicates.

Step-by-Step Algorithm

  1. Step 1: Initialize an array `ugly` of size `n` to store the ugly numbers. Initialize `ugly[0]` to 1, as 1 is the first ugly number.
  2. Step 2: Initialize three pointers `p2`, `p3`, and `p5` to 0. These pointers track the indices of the ugly numbers that will be multiplied by 2, 3, and 5, respectively.
  3. Step 3: Iterate from `i = 1` to `n - 1` to calculate the remaining ugly numbers.
  4. Step 4: Calculate `c2 = ugly[p2] * 2`, `c3 = ugly[p3] * 3`, and `c5 = ugly[p5] * 5`. These are the candidate ugly numbers.
  5. Step 5: Find the minimum of `c2`, `c3`, and `c5`. This minimum value is the next ugly number `ugly[i]`.
  6. Step 6: Increment the pointer(s) corresponding to the factor(s) that produced the minimum value. This is crucial to avoid generating duplicates. For instance, if `c2` and `c3` are both equal to the minimum, increment both `p2` and `p3`.
  7. Step 7: After the loop, the n-th ugly number is stored in `ugly[n - 1]`.

Key Insights

  • Insight 1: Ugly numbers can be generated by multiplying existing ugly numbers by 2, 3, or 5.
  • Insight 2: Dynamic programming can be used to store and build the sequence of ugly numbers efficiently.
  • Insight 3: Maintaining separate pointers for each prime factor (2, 3, 5) allows us to generate the next ugly number in increasing order without redundant calculations or sorting.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Hash Table, Math, Dynamic Programming, Heap (Priority Queue).

Companies

Asked at: Accenture, EPAM Systems.