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 <= 1690When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine we need to find a specific special number in a giant sorted sequence of numbers that only have certain allowed factors. The brute force approach checks every single whole number one by one, starting from the very beginning, to see if it qualifies.
Here's how the algorithm would work step-by-step:
def nth_ugly_number(target_position): valid_number_count = 0 current_number = 0 # We must iterate sequentially because the sequence of ugly numbers has no arithmetic shortcut while valid_number_count < target_position: current_number += 1 temp_number = current_number for prime_factor in [2, 3, 5]: while temp_number % prime_factor == 0: temp_number //= prime_factor # If division completely reduces the number to one, it consists solely of allowed factors if temp_number == 1: valid_number_count += 1 # Halting execution guarantees we isolate the exact target index requested by the prompt return current_numberInstead of checking every single number to see if it is ugly, the optimal approach builds the sequence of ugly numbers in order from smallest to largest. By using the rule that every ugly number is simply a previous ugly number multiplied by two, three, or five, we can strategically generate the next correct number every time.
Here's how the algorithm would work step-by-step:
class Solution:
def nthUglyNumber(self, nth_ugly_target: int) -> int:
ugly_sequence = [1]
pointer_for_two = 0
pointer_for_three = 0
pointer_for_five = 0
while len(ugly_sequence) < nth_ugly_target:
next_val_two = ugly_sequence[pointer_for_two] * 2
next_val_three = ugly_sequence[pointer_for_three] * 3
next_val_five = ugly_sequence[pointer_for_five] * 5
# Selecting the absolute minimum ensures the sequence remains strictly sorted.
next_ugly_number = min(next_val_two, next_val_three, next_val_five)
ugly_sequence.append(next_ugly_number)
# Advancing pointers ensures each multiplier contributes to future unique candidates.
if next_ugly_number == next_val_two:
pointer_for_two += 1
if next_ugly_number == next_val_three:
pointer_for_three += 1
# Independent checks allow duplicate values to be skipped via simultaneous advancement.
if next_ugly_number == next_val_five:
pointer_for_five += 1
return ugly_sequence[-1]| Case | How to Handle |
|---|---|
| Minimum input where n equals 1 | The algorithm should immediately initialize and return 1 as the first ugly number without entering the loop. |
| Maximum valid input constraints such as n equals 1690 | A dynamic programming approach with three pointers scales efficiently to O(n) time complexity to compute the result. |
| Integer overflow during multiplication by 2, 3, or 5 | Using 64-bit integers (long in Java/C++ or standard integers in Python) prevents arithmetic overflow during calculations. |
| Duplicate values generated by multiple prime multipliers simultaneously | Using independent conditional checks for the pointers rather than else-if branches ensures duplicate products are skipped. |
| Zero or negative inputs for n | Problem constraints specify n is a positive integer, so these can be assumed invalid or handled by returning an error. |