# leetcode Ugly Number II

This is not a difficult one.
Basic idea is, assum that we have a self-sorted queue. Everytime, we choose the smallest one n, insert 2*n , 3*n , 5*n into this queue. After n times, we will get the n-th Ugly number.
The time will be $O(nlogn)$. Do we have a better way? The answer is YES!

Remeber the key point is we need to always get the smallest number of this queue. If we use three queue, every time we choose the smallest number of this three queue rears n. And put 2*n to the L2 queue. put 3*n to the L3 queue, put 5*n to the L5 queue. We can confirm that these queues are in order. Because every time the choosen number n is larger than the one before that. So the in every queue, the new numbers will be the largest one.

Contents