Ugly Number II

An ugly number is a positive integer whose only prime factors are 2, 3 and 5 (1 counts as ugly). Given an integer n, return the nth ugly number.

30:00

Ugly Number II
medium
Free written solution
Topics
Companies

An ugly number is a positive integer whose only prime factors are 2, 3 and 5 (1 counts as ugly). Given an integer n, return the nth ugly number.

Example 1:
Input: {"n":10}
Output: 12
Constraints:
  • 1≤n≤16901 \leq n \leq 1690

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"n":10}

ugly[0] = 1: the smallest ugly number. Every later one is some earlier ugly number times 2, 3 or 5.

three candidate lanes

×2

i2 = 0

1×2

2

×3

i3 = 0

1×3

3

×5

i5 = 0

1×5

5

ugly[] (1 of 10 filled)

1

0

▲i2
▲i3
▲i5
?

1

newest ugly number: 1

Variables
VariableValue
n10
ugly[0]1
DepthFunction Call
Stack empty
0/32