✅Easy and Clean Code (C++ / Java / JavaScript / C) Beats 100% ✅✅✅
Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem at hand is to find the n-th "ugly number." An ugly number is defined as a n
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem at hand is to find the n-th "ugly number." An ugly number is defined as a number whose only prime factors are 2, 3, or 5. The idea here is to generate ugly numbers in a sequential manner.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1.\tInitialization:\n\u2022\tA vector t of size n+1 is used to store the first n ugly numbers, with t[1] initialized to 1 (since 1 is considered the first ugly number).\n\u2022\tThree pointers, t2, t3, and t5, are used to keep track of the position in the list for generating the next ugly number by multiplying by 2, 3, and 5, respectively.\n2.\tGenerating Ugly Numbers:\n\u2022\tIn each iteration, compute potential next ugly numbers by multiplying the current values pointed to by t2, t3, and t5 with 2, 3, and 5, respectively.\n\u2022\tThe next ugly number t[i] is the minimum of these potential values.\n\u2022\tUpdate the pointers t2, t3, and t5 as needed:\no\tIf t[i] matches the value generated by multiplying t[t2] by 2, increment t2.\no\tIf t[i] matches the value generated by multiplying t[t3] by 3, increment t3.\no\tIf t[i] matches the value generated by multiplying t[t5] by 5, increment t5.\n3.\tReturn:\n\u2022\tAfter the loop completes, t[n] will be the nnn-th ugly number.\n\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n        The time complexity of this approach is O(n).\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n        O(n)\n# Code\n```\nclass Solution {\npublic:\n    int nthUglyNumber(int n) {\n        vector<int> t(n + 1); //To handle edge cases\n        t[1] = 1; // First ugly number\n        // Initialize t2, t3 & t5 with 1 to pointing the 1st ugly number\n        int t2 = 1;\n        int t3 = 1;\n        int t5 = 1;\n        for (int i = 2; i <= n; i++) {\n            int second = t[t2] * 2;\n            int third = t[t3] * 3;\n            int fifth = t[t5] * 5;\n            t[i] = min({second, third, fifth});\n            if (t[i] == second) {\n                t2++;\n            }\n            if (t[i] == third) {\n                t3++;\n            }\n            if (t[i] == fifth) {\n                t5++;\n            }\n        }\n        return t[n];\n    }\n};\n```\n![upvote.jpeg.jpg](https://assets.leetcode.com/users/images/efc8400c-8748-44db-ac84-e7b5253fa1d6_1723958552.877168.jpeg)\n
['C++']