Skip to content

Commit 8c82e57

Browse files
authored
Merge pull request #1164 from nishant0708/Q263-264
[Feature Request]: Leetcode 263(Easy)-264(Medium) Solution #1100
2 parents 49be46a + e51a096 commit 8c82e57

File tree

2 files changed

+297
-0
lines changed

2 files changed

+297
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
---
2+
id: ugly-number
3+
title: Ugly Number
4+
sidebar_label: 0263-Ugly-Number
5+
tags:
6+
- Math
7+
- Number Theory
8+
- C++
9+
- Java
10+
- Python
11+
description: "This document provides a solution to the Ugly Number problem, where we need to determine if a number is an ugly number."
12+
---
13+
14+
## Problem
15+
16+
An ugly number is a positive integer whose prime factors are limited to 2, 3, and 5. Given an integer `n`, return `true` if `n` is an ugly number.
17+
18+
### Examples
19+
20+
**Example 1:**
21+
22+
Input: n = 6
23+
Output: true
24+
Explanation: 6 = 2 × 3
25+
26+
**Example 2:**
27+
28+
Input: n = 8
29+
Output: true
30+
Explanation: 8 = 2 × 2 × 2
31+
32+
**Example 3:**
33+
34+
Input: n = 14
35+
Output: false
36+
Explanation: 14 is not an ugly number since it includes the prime factor 7.
37+
38+
**Example 4:**
39+
40+
Input: n = 1
41+
Output: true
42+
Explanation: 1 is typically treated as an ugly number.
43+
44+
### Constraints
45+
46+
- `-2^31 <= n <= 2^31 - 1`
47+
48+
### Approach
49+
50+
To determine if a number is an ugly number, we can follow these steps:
51+
52+
1. If `n` is less than or equal to 0, return `false` since ugly numbers are positive.
53+
2. Divide `n` by 2, 3, and 5 as long as it is divisible by these numbers.
54+
3. After performing the above division, if the resulting number is 1, then `n` is an ugly number. Otherwise, it is not.
55+
56+
### Solution
57+
58+
#### Code in Different Languages
59+
60+
### C++ Solution
61+
```cpp
62+
#include <iostream>
63+
64+
using namespace std;
65+
66+
bool isUgly(int n) {
67+
if (n <= 0) return false;
68+
while (n % 2 == 0) n /= 2;
69+
while (n % 3 == 0) n /= 3;
70+
while (n % 5 == 0) n /= 5;
71+
return n == 1;
72+
}
73+
74+
int main() {
75+
int n = 6;
76+
cout << (isUgly(n) ? "true" : "false") << endl;
77+
}
78+
```
79+
### Java Solution
80+
81+
```java
82+
public class UglyNumber {
83+
public static boolean isUgly(int n) {
84+
if (n <= 0) return false;
85+
while (n % 2 == 0) n /= 2;
86+
while (n % 3 == 0) n /= 3;
87+
while (n % 5 == 0) n /= 5;
88+
return n == 1;
89+
}
90+
91+
public static void main(String[] args) {
92+
int n = 6;
93+
System.out.println(isUgly(n) ? "true" : "false");
94+
}
95+
}
96+
```
97+
### Python Solution
98+
99+
```python
100+
def isUgly(n):
101+
if n <= 0:
102+
return False
103+
for i in [2, 3, 5]:
104+
while n % i == 0:
105+
n //= i
106+
return n == 1
107+
108+
n = 6
109+
print(isUgly(n))
110+
```
111+
### Complexity Analysis
112+
**Time Complexity:** O(logN)
113+
>Reason: The division operations will run in logarithmic time relative to the input value n.
114+
115+
**Space Complexity:** O(1)
116+
117+
>Reason: We use a constant amount of extra space.
118+
119+
This solution checks whether a number is an ugly number by continually dividing the number by 2, 3, and 5. If the result is 1, the number is an ugly number. The time complexity is logarithmic, and the space complexity is constant, making it efficient for large input values.
120+
121+
### References
122+
**LeetCode Problem:** Ugly Number
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
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

Comments
 (0)