The idea is to use an array to store the ugly numbers and use three pointers for 2, 3, and 5 to calculate the next potential ugly numbers. We then choose the minimum of these numbers to be the next ugly number and appropriately move the pointers.
Time Complexity: O(n)
, where n
is the number of ugly numbers to generate.
Space Complexity: O(n)
, for storing the ugly numbers array.
1def nthUglyNumber(n):
2 ugly = [0] * n
3 ugly[0] = 1
4 i2, i3, i5 = 0, 0, 0
5 next_2, next_3, next_5 = 2, 3, 5
6
7 for i in range(1, n):
8 next_ugly = min(next_2, next_3, next_5)
9 ugly[i] = next_ugly
10
11 if next_ugly == next_2:
12 i2 += 1
13 next_2 = ugly[i2] * 2
14 if next_ugly == next_3:
15 i3 += 1
16 next_3 = ugly[i3] * 3
17 if next_ugly == next_5:
18 i5 += 1
19 next_5 = ugly[i5] * 5
20
21 return ugly[-1]
22
23n = 10
24print(nthUglyNumber(n)) # Output: 12
25
The Python solution uses a list to store ugly numbers and three indices corresponding to the factors 2, 3, and 5. In each iteration, it calculates the next potential ugly numbers, updates the array, and adjusts the indices accordingly.
This method involves using a min-heap to manage the sequence of potential ugly numbers. We start with 1 in the min-heap and repeatedly extract the smallest element, multiplying it by 2, 3, and 5 to generate new candidates, which are then inserted back into the heap. Duplicate entries are avoided by using a set for tracking which numbers have been added to the heap.
Time Complexity: O(n log n)
, primarily due to heap operations.
Space Complexity: O(n)
, for data structures that might store up to n numbers.
1#include <iostream>
2#include <queue>
3#include <unordered_set>
4using namespace std;
5
6int nthUglyNumber(int n) {
7 priority_queue<long, vector<long>, greater<long>> minHeap;
8 unordered_set<long> seen;
9 minHeap.push(1);
10 seen.insert(1);
11 long currUgly = 1;
12
13 for (int i = 0; i < n; ++i) {
14 currUgly = minHeap.top();
15 minHeap.pop();
16 if (!seen.count(currUgly * 2)) {
17 minHeap.push(currUgly * 2);
18 seen.insert(currUgly * 2);
19 }
20 if (!seen.count(currUgly * 3)) {
21 minHeap.push(currUgly * 3);
22 seen.insert(currUgly * 3);
23 }
24 if (!seen.count(currUgly * 5)) {
25 minHeap.push(currUgly * 5);
26 seen.insert(currUgly * 5);
27 }
28 }
29 return (int)currUgly;
30}
31
32int main() {
33 int n = 10;
34 cout << nthUglyNumber(n) << endl; // Output: 12
35 return 0;
36}
The solution uses a min-heap to keep track of and retrieve the smallest ugly number, avoiding duplicates by using a set. It generates ugly numbers by multiplying the current smallest number by 2, 3, and 5, inserting valid new numbers back into the heap.