An ugly number is a fascinating mathematical concept - it's a positive integer whose only prime factors are 2, 3, and 5. In other words, ugly numbers can only be formed by multiplying powers of 2, 3, and 5 together!
The sequence starts as: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12...
Notice how 7 is missing (it's prime), 11 is missing (also prime), and 14 is missing (contains factor 7).
Your task: Given an integer n, return the nth ugly number in this sequence.
Goal: Efficiently generate the sequence of ugly numbers and find the nth one.
Input & Output
Visualization
Time & Space Complexity
We generate exactly n ugly numbers, each in constant time
We store all n ugly numbers in an array
Constraints
- 1 โค n โค 1690
- The answer is guaranteed to fit in a 32-bit signed integer
- All ugly numbers must be positive integers