Sharing very simple and elegant Python solution using heap, with explanation
import heapq\n    \n    class Solution(object):\n        \n        # The idea is to keep a heap that stores a tuple (val, fact).\n        # The fact value store
    import heapq\n    \n    class Solution(object):\n        \n        # The idea is to keep a heap that stores a tuple (val, fact).\n        # The fact value stores the last factor the number was multiplied to.\n        # So every time I extract the minimum and look at the queue, I know\n        # it must be multiplied by queue and next factor, otherwise I will have a\n        # duplicate. \n        #\n        # For example, we have 2 and 5 with fact 2 and fact 5. When I extract\n        # 2 and fact 2, I will insert 2 * 5 = 10 with fact 5. Then, when I extract 5 from queue\n        # 5, if I do 5 * 2 (fact 2) = 10,\n        # I will have a duplicate. So every number coming from\n        # the k-th fact must be multiplied only by the factor fact and higher.\n        \n        \n        \n        def nthUglyNumber(self, n):\n            if n is 1:\n                return 1\n            h, val = [(2, 2), (3, 3), (5, 5)], 1\n            for i in range(1, n):\n                val, fact = heapq.heappop(h)\n                heapq.heappush(h, (val * 5, 5))\n                if fact <= 3:\n                    heapq.heappush(h, (val * 3, 3))\n                if fact is 2:\n                    heapq.heappush(h, (val * 2, 2))\n            return val
[]