Python 7 lines O(N) using collections.deque
Keep picking the smallest value from the start of queues, popping it out to the left, and appending bigger candidates to the end.\n\n    def nthUglyNumber(self,
Keep picking the smallest value from the start of queues, popping it out to the left, and appending bigger candidates to the end.\n```\n    def nthUglyNumber(self, n):\n        queues = {prime: collections.deque([1]) for prime in [2, 3, 5]}\n        for _ in range(n):\n            result = min([queue[0] for queue in queues.values()])\n            for prime, queue in queues.items():\n                if result == queue[0]: queue.popleft()\n                queue.append(result*prime)\n        return result\n```\nThe code below does this same thing, but written less succinctly. Maybe slighlty easier to follow.\n```\nfrom collections import deque\nclass Solution(object):\n    def nthUglyNumber(self, n):\n        if n == 1: return 1\n        queue2, queue3, queue5 = deque([2]), deque([3]), deque([5])\n        for _ in range(1, n):\n            result = min(queue2[0], queue3[0], queue5[0])\n            if result == queue2[0]: queue2.popleft()\n            if result == queue3[0]: queue3.popleft()\n            if result == queue5[0]: queue5.popleft()\n            queue2.append(result*2)\n            queue3.append(result*3)\n            queue5.append(result*5)\n        return result\n```\n
['Python']