mediumHeapDynamic ProgrammingHeap / Priority Queue

Ugly Number II

mediumTime: O(n log n)Space: O(n)

Recognize the pattern

nth ugly numberonly prime factors 2, 3, 5merge three sequences

Brute force idea

A straightforward first read of Ugly Number II is this: Check each number 1, 2, 3,.. for ugliness. Very slow for large n. That instinct is useful because it follows the prompt literally, but it usually keeps revisiting work the problem is begging you to organize.

Better approach

A calmer way to see Ugly Number II is this: Three pointers: track the next ugly number to multiply by 2, 3, and 5. Take the minimum of the three candidates. Advance the pointer(s) that produced the minimum. The goal is not to be clever for its own sake, but to remember the one relationship that keeps the solution grounded as you move forward.

Key invariant

The truth you want to protect throughout Ugly Number II is this: Every ugly number is 2, 3, or 5 times a previous ugly number. Three sorted sequences (ugly×2, ugly×3, ugly×5) are merged, and the minimum gives the next ugly number. If that remains true after every update, the rest of the reasoning has a stable place to stand.

Watch out for

A common way to get lost in Ugly Number II is this: Not advancing ALL pointers that match the minimum — if ugly[i2]×2 == ugly[i3]×3, advance both to avoid duplicates. Most mistakes here are not about syntax; they come from losing track of what your state, pointer, or structure is supposed to mean.

Heap / Priority Queue Pattern