264. Ugly Number II LeetCode Solution

In this guide, you will get 264. Ugly Number II LeetCode Solution with the best time and space complexity. The solution to Ugly Number II problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Ugly Number II solution in C++
  4. Ugly Number II solution in Java
  5. Ugly Number II solution in Python
  6. Additional Resources
264. Ugly Number II LeetCode Solution image

Problem Statement of Ugly Number II

An ugly number is a positive integer whose prime factors are limited to 2, 3, and 5.
Given an integer n, return the nth 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

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

264. Ugly Number II LeetCode Solution in C++

class Solution {
 public:
  int nthUglyNumber(int n) {
    vector<int> uglyNums{1};
    int i2 = 0;
    int i3 = 0;
    int i5 = 0;

    while (uglyNums.size() < n) {
      const int next2 = uglyNums[i2] * 2;
      const int next3 = uglyNums[i3] * 3;
      const int next5 = uglyNums[i5] * 5;
      const int next = min({next2, next3, next5});
      if (next == next2)
        ++i2;
      if (next == next3)
        ++i3;
      if (next == next5)
        ++i5;
      uglyNums.push_back(next);
    }

    return uglyNums.back();
  }
};
/* code provided by PROGIEZ */

264. Ugly Number II LeetCode Solution in Java

class Solution {
  public int nthUglyNumber(int n) {
    List<Integer> uglyNums = new ArrayList<>();
    uglyNums.add(1);
    int i2 = 0;
    int i3 = 0;
    int i5 = 0;

    while (uglyNums.size() < n) {
      final int next2 = uglyNums.get(i2) * 2;
      final int next3 = uglyNums.get(i3) * 3;
      final int next5 = uglyNums.get(i5) * 5;
      final int next = Math.min(next2, Math.min(next3, next5));
      if (next == next2)
        ++i2;
      if (next == next3)
        ++i3;
      if (next == next5)
        ++i5;
      uglyNums.add(next);
    }

    return uglyNums.get(uglyNums.size() - 1);
  }
}
// code provided by PROGIEZ

264. Ugly Number II LeetCode Solution in Python

class Solution:
  def nthUglyNumber(self, n: int) -> int:
    nums = [1]
    i2 = 0
    i3 = 0
    i5 = 0

    while len(nums) < n:
      next2 = nums[i2] * 2
      next3 = nums[i3] * 3
      next5 = nums[i5] * 5
      next = min(next2, next3, next5)
      if next == next2:
        i2 += 1
      if next == next3:
        i3 += 1
      if next == next5:
        i5 += 1
      nums.append(next)

    return nums[-1]
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.