0704 | Ugly Number II | O(N) C++ Solution
Approach\nEach time, we take the least of multiple of 2, 3, 5 and keep generating from the already-bulit array, thus ensuring the number is always ugly.\n\n\n##
## Approach\nEach time, we take the least of multiple of 2, 3, 5 and keep generating from the already-bulit array, thus ensuring the number is always ugly.\n![image](https://assets.leetcode.com/users/images/da9be94a-e226-4a27-8978-92d8d0923342_1593877426.4611287.png)\n\n## Complexity Analysis\nTime: O(N)\nSpace: O(N)\n\n```cpp\nclass Solution {\npublic:\n    int nthUglyNumber(int n) {\n        int two = 0, three = 0, five = 0, next = 0;\n        vector<int> res(1, 1);\n        n -= 1;\n        while(n--){\n            next = min(res[two] * 2, min(res[three] * 3, res[five] * 5));\n            if(next == res[two] * 2){\n                two++;\n            }\n            if(next == res[three] * 3){\n                three++;\n            }\n            if(next == res[five] * 5){\n                five++;\n            }\n            res.push_back(next);\n        }\n        \n        return res.back();\n    }\n};\n```
['C']