All problems
Ugly Number II
Problem
Return the n-th ugly number, where ugly numbers have only 2, 3 and 5 as prime factors and 1 is the first ugly number.
Maintain three pointers into the growing list for the next multiples of 2, 3 and 5, always appending the smallest next value and advancing every pointer that produced it. This keeps the list sorted and duplicate-free.
Hint
Advance every pointer that matches the appended value so 6 is generated once, not twice.
Test cases
case 1
in: [1]
expected: 1
case 2
in: [10]
expected: 12
case 3
in: [12]
expected: 16
case 4
in: [15]
expected: 24
case 5
in: [20]
expected: 36
Starter codePython
def nth_ugly_number(n):
# Your code here
pass5 test cases · run your code against them in the editor