|
| 1 | +--- |
| 2 | +id: ugly-number-II |
| 3 | +title: Ugly Number II |
| 4 | +sidebar_label: 0264-Ugly-Number-II |
| 5 | +tags: |
| 6 | + - Math |
| 7 | + - Number Theory |
| 8 | + - Dynamic Programming |
| 9 | + - Heap |
| 10 | + - C++ |
| 11 | + - Java |
| 12 | + - Python |
| 13 | +description: "This document provides a solution to the Ugly Number II problem, where we need to find the nth ugly number." |
| 14 | +--- |
| 15 | + |
| 16 | +## Problem |
| 17 | + |
| 18 | +An ugly number is a positive integer whose prime factors are limited to 2, 3, and 5. Given an integer `n`, return the `n`-th ugly number. |
| 19 | + |
| 20 | +### Examples |
| 21 | + |
| 22 | +**Example 1:** |
| 23 | + |
| 24 | +Input: n = 10 |
| 25 | +Output: 12 |
| 26 | +Explanation: The sequence of ugly numbers is [1, 2, 3, 4, 5, 6, 8, 9, 10, 12, ...], and the 10th ugly number is 12. |
| 27 | + |
| 28 | +**Example 2:** |
| 29 | + |
| 30 | +Input: n = 1 |
| 31 | +Output: 1 |
| 32 | +Explanation: 1 is typically treated as an ugly number. |
| 33 | + |
| 34 | +### Constraints |
| 35 | + |
| 36 | +- `1 <= n <= 1690` |
| 37 | + |
| 38 | +### Approach |
| 39 | + |
| 40 | +To solve this problem, we can use a dynamic programming approach to generate ugly numbers in sequence: |
| 41 | + |
| 42 | +1. Initialize an array `ugly_numbers` to store the first `n` ugly numbers. |
| 43 | +2. Use three pointers (i2, i3, i5) to track the indices for multiples of 2, 3, and 5 respectively. |
| 44 | +3. Start with the first ugly number `1` and iteratively compute the next ugly number by taking the minimum of the next multiples of 2, 3, and 5. |
| 45 | +4. Update the corresponding pointer and move to the next position in the `ugly_numbers` array. |
| 46 | +5. Repeat until we have generated `n` ugly numbers. |
| 47 | + |
| 48 | +### Solution |
| 49 | + |
| 50 | +#### Code in Different Languages |
| 51 | + |
| 52 | +### C++ Solution |
| 53 | +```cpp |
| 54 | +#include <iostream> |
| 55 | +#include <vector> |
| 56 | + |
| 57 | +using namespace std; |
| 58 | + |
| 59 | +int nthUglyNumber(int n) { |
| 60 | + vector<int> ugly_numbers(n); |
| 61 | + ugly_numbers[0] = 1; |
| 62 | + int i2 = 0, i3 = 0, i5 = 0; |
| 63 | + int next_multiple_of_2 = 2; |
| 64 | + int next_multiple_of_3 = 3; |
| 65 | + int next_multiple_of_5 = 5; |
| 66 | + |
| 67 | + for (int i = 1; i < n; i++) { |
| 68 | + int next_ugly = min(next_multiple_of_2, min(next_multiple_of_3, next_multiple_of_5)); |
| 69 | + ugly_numbers[i] = next_ugly; |
| 70 | + |
| 71 | + if (next_ugly == next_multiple_of_2) { |
| 72 | + i2++; |
| 73 | + next_multiple_of_2 = ugly_numbers[i2] * 2; |
| 74 | + } |
| 75 | + if (next_ugly == next_multiple_of_3) { |
| 76 | + i3++; |
| 77 | + next_multiple_of_3 = ugly_numbers[i3] * 3; |
| 78 | + } |
| 79 | + if (next_ugly == next_multiple_of_5) { |
| 80 | + i5++; |
| 81 | + next_multiple_of_5 = ugly_numbers[i5] * 5; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return ugly_numbers[n - 1]; |
| 86 | +} |
| 87 | + |
| 88 | +int main() { |
| 89 | + int n = 10; |
| 90 | + cout << nthUglyNumber(n) << endl; // Output: 12 |
| 91 | +} |
| 92 | +``` |
| 93 | +### Java Solution |
| 94 | +```java |
| 95 | +public class UglyNumberII { |
| 96 | + public static int nthUglyNumber(int n) { |
| 97 | + int[] ugly_numbers = new int[n]; |
| 98 | + ugly_numbers[0] = 1; |
| 99 | + int i2 = 0, i3 = 0, i5 = 0; |
| 100 | + int next_multiple_of_2 = 2; |
| 101 | + int next_multiple_of_3 = 3; |
| 102 | + int next_multiple_of_5 = 5; |
| 103 | + |
| 104 | + for (int i = 1; i < n; i++) { |
| 105 | + int next_ugly = Math.min(next_multiple_of_2, Math.min(next_multiple_of_3, next_multiple_of_5)); |
| 106 | + ugly_numbers[i] = next_ugly; |
| 107 | + |
| 108 | + if (next_ugly == next_multiple_of_2) { |
| 109 | + i2++; |
| 110 | + next_multiple_of_2 = ugly_numbers[i2] * 2; |
| 111 | + } |
| 112 | + if (next_ugly == next_multiple_of_3) { |
| 113 | + i3++; |
| 114 | + next_multiple_of_3 = ugly_numbers[i3] * 3; |
| 115 | + } |
| 116 | + if (next_ugly == next_multiple_of_5) { |
| 117 | + i5++; |
| 118 | + next_multiple_of_5 = ugly_numbers[i5] * 5; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return ugly_numbers[n - 1]; |
| 123 | + } |
| 124 | +
|
| 125 | + public static void main(String[] args) { |
| 126 | + int n = 10; |
| 127 | + System.out.println(nthUglyNumber(n)); // Output: 12 |
| 128 | + } |
| 129 | +} |
| 130 | +``` |
| 131 | +### Python Solution |
| 132 | + |
| 133 | +```python |
| 134 | +def nthUglyNumber(n): |
| 135 | + ugly_numbers = [0] * n |
| 136 | + ugly_numbers[0] = 1 |
| 137 | + i2 = i3 = i5 = 0 |
| 138 | + next_multiple_of_2 = 2 |
| 139 | + next_multiple_of_3 = 3 |
| 140 | + next_multiple_of_5 = 5 |
| 141 | + |
| 142 | + for i in range(1, n): |
| 143 | + next_ugly = min(next_multiple_of_2, next_multiple_of_3, next_multiple_of_5) |
| 144 | + ugly_numbers[i] = next_ugly |
| 145 | + |
| 146 | + if next_ugly == next_multiple_of_2: |
| 147 | + i2 += 1 |
| 148 | + next_multiple_of_2 = ugly_numbers[i2] * 2 |
| 149 | + if next_ugly == next_multiple_of_3: |
| 150 | + i3 += 1 |
| 151 | + next_multiple_of_3 = ugly_numbers[i3] * 3 |
| 152 | + if next_ugly == next_multiple_of_5: |
| 153 | + i5 += 1 |
| 154 | + next_multiple_of_5 = ugly_numbers[i5] * 5 |
| 155 | + |
| 156 | + return ugly_numbers[n - 1] |
| 157 | + |
| 158 | +n = 10 |
| 159 | +print(nthUglyNumber(n)) # Output: 12 |
| 160 | +``` |
| 161 | +### Complexity Analysis |
| 162 | +**Time Complexity:** O(n) |
| 163 | + |
| 164 | +>Reason: We iterate through the sequence of ugly numbers up to n. |
| 165 | +
|
| 166 | +**Space Complexity:** O(n) |
| 167 | + |
| 168 | +>Reason: We use an array to store the first n ugly numbers. |
| 169 | +
|
| 170 | +This solution efficiently finds the n-th ugly number by generating the sequence of ugly numbers using a dynamic programming approach with three pointers for multiples of 2, 3, and 5. |
| 171 | + |
| 172 | +### References |
| 173 | +**LeetCode Problem:** Ugly Number II |
| 174 | + |
| 175 | + |
0 commit comments