Ugly Number II - Problem
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.
For example, the first 10 ugly numbers are: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12. Note that 1 is typically treated as an ugly number.
Input & Output
Example 1 — Finding 10th Ugly Number
$
Input:
n = 10
›
Output:
12
💡 Note:
The first 10 ugly numbers are: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12. The 10th ugly number is 12.
Example 2 — First Ugly Number
$
Input:
n = 1
›
Output:
1
💡 Note:
1 is conventionally treated as the first ugly number.
Example 3 — Edge Case with Small n
$
Input:
n = 6
›
Output:
6
💡 Note:
The first 6 ugly numbers are: 1, 2, 3, 4, 5, 6. The 6th is 6 (which is 2 × 3).
Constraints
- 1 ≤ n ≤ 1690
Visualization
Tap to expand
Understanding the Visualization
1
Input
Given n, find the nth ugly number
2
Generate
Build sequence using only factors 2, 3, 5
3
Output
Return the nth number in the sequence
Key Takeaway
🎯 Key Insight: Generate ugly numbers in order using three pointers for multiples of 2, 3, and 5
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code