Trains the technique from
LeetCode 264Ugly Number IIThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A workshop assembles pulley blocks out of stages. One stage doubles the drive, another triples it and a third quintuples it, and a block may stack any number of stages in any mix, including no stages at all.
Call a positive integer a reachable factor when some stack of stages multiplies out to exactly that integer. A block with no stages leaves the drive alone, so the smallest reachable factor is one.
Sort the reachable factors into increasing order with no repeats, and return the one in position n, counting the smallest as position 1.
Example 1
The reachable factors below 8 are 1, 2, 3, 4, 5 and 6, six of them, so 8 lands in position 7. No stack reaches 7, because a stage never multiplies by 7.
Example 2
Between 6 and 10 the only reachable factors are 8 and 9, so 10 lands in position 9. A doubling stage on top of a quintupling stage gives 10.
Example 3
Position 1 holds the empty stack, which leaves the factor at 1, so a single doubling stage puts 2 in position 2.
Example 4
Two doubling stages and two tripling stages multiply out to 36, and exactly 19 reachable factors are smaller than it.
Example 5
Four doubling stages and two tripling stages multiply out to 144, and exactly 39 reachable factors are smaller than it.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def nth_ugly_number(n: int) -> int:public int nthUglyNumber(int n)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.