💯🔥Daily Question 1️⃣8️⃣ Aug, Here is simple n explained Solution using pointers in java,python3 🐍
Intuition\n Describe your first thoughts on how to solve this problem. \nHere\u2019s a short and simple explanation of the code:\n\n1. Track Ugly Numbers: The c
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nHere\u2019s a short and simple explanation of the code:\n\n1. **Track Ugly Numbers:** The code finds the `n`th ugly number (a number whose prime factors are only 2, 3, or 5).\n\n2. **Pointers:** It uses three pointers (`t2`, `t3`, `t5`) to track multiples of 2, 3, and 5.\n\n3. **Generate Ugly Numbers:** For each position in the array, calculate the next ugly number as the smallest value among the current multiples of 2, 3, and 5.\n\n4. **Update Pointers:** When a multiple is used, move the respective pointer forward.\n\n5. **Return the nth Ugly Number:** The nth ugly number is the last element in the array.\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n\n1. **Base Cases:**\n   - Return 0 if `n <= 0` (invalid input).\n   - Return 1 if `n == 1` (first ugly number is 1).\n\n2. **Initialize Pointers:**\n   - `t2`, `t3`, `t5` track indices for multiples of 2, 3, and 5.\n   - Array `k` stores the sequence of ugly numbers.\n\n3. **Iterate to Find Ugly Numbers:**\n   - For each index `i`, find the next ugly number as the minimum of:\n     - `k[t2] * 2`, `k[t3] * 3`, and `k[t5] * 5`.\n   - Increment the respective pointer (`t2`, `t3`, or `t5`) if its multiple was chosen.\n\n4. **Return the Result:**\n   - The nth ugly number is at `k[n-1]`.\n\n# Complexity\n- Time complexity:O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(n)\n# Code\n```java []\nclass Solution {\n    public int nthUglyNumber(int n) {\n        if (n <= 0) return 0; \n        if (n == 1) return 1; \n        \n        int t2 = 0, t3 = 0, t5 = 0; \n        int[] k = new int[n];\n        k[0] = 1;\n        \n        for (int i = 1; i < n; i++) {\n            k[i] = Math.min(k[t2] * 2, Math.min(k[t3] * 3, k[t5] * 5));\n            \n            if (k[i] == k[t2] * 2) t2++;\n            if (k[i] == k[t3] * 3) t3++;\n            if (k[i] == k[t5] * 5) t5++;\n        }\n        \n        return k[n - 1];\n    }\n}\n```\n```python3 []\nclass Solution:\n    def nthUglyNumber(self, n: int) -> int:\n        if n <= 0:\n            return 0\n        if n == 1:\n            return 1\n        \n        t2 = t3 = t5 = 0\n        k = [0] * n\n        k[0] = 1\n        \n        for i in range(1, n):\n            k[i] = min(k[t2] * 2, k[t3] * 3, k[t5] * 5)\n            \n            if k[i] == k[t2] * 2:\n                t2 += 1\n            if k[i] == k[t3] * 3:\n                t3 += 1\n            if k[i] == k[t5] * 5:\n                t5 += 1\n        \n        return k[-1]\n\n```\n\nplz upvote, if you like the explanation\n\n![image.png](https://assets.leetcode.com/users/images/cd0c5a2e-416c-4685-a1f5-4c7e85c6017d_1723941959.8766382.png)\n\n\n\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n
['Math', 'Dynamic Programming', 'Heap (Priority Queue)', 'Java', 'Python3']