264. Ugly Number II (Medium)
Write a program to find the n
-th ugly number.
Ugly numbers are positive numbers whose prime factors only include 2, 3, 5
.
Example:
Input: n = 10 Output: 12 Explanation:1, 2, 3, 4, 5, 6, 8, 9, 10, 12
is the sequence of the first10
ugly numbers.
Note:
1
is typically treated as an ugly number.n
does not exceed 1690.
Related Topics
[Heap] [Math] [Dynamic Programming]
Similar Questions
- Merge k Sorted Lists (Hard)
- Count Primes (Easy)
- Ugly Number (Easy)
- Perfect Squares (Medium)
- Super Ugly Number (Medium)
Hints
Hint 1
The naive approach is to callisUgly
for every number until you reach the nth one. Most numbers are not ugly. Try to focus your effort on generating only the ugly ones.