Skip to content

Commit b37989e

Browse files
committed
leetcode
1 parent 0e2c704 commit b37989e

File tree

4 files changed

+305
-0
lines changed

4 files changed

+305
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
103103
- [Count and Say](CountAndSay/count_and_say.dart)
104104
- [Top K Frequent Words](TopKFrequentWords/top_k_frequent_words.dart)
105105
- [Integer to Roman](IntegerToRoman/integer_to_roman.dart)
106+
- [Ugly Number](UglyNumber/ugly_number.dart)
106107

107108
## Reach me via
108109

UglyNumber/ugly_number.dart

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
3+
4+
5+
-* Ugly Number *-
6+
7+
An ugly number is a positive integer whose prime factors are limited to 2, 3, and 5.
8+
9+
Given an integer n, return true if n is an ugly number.
10+
11+
12+
13+
Example 1:
14+
15+
Input: n = 6
16+
Output: true
17+
Explanation: 6 = 2 × 3
18+
Example 2:
19+
20+
Input: n = 1
21+
Output: true
22+
Explanation: 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
23+
Example 3:
24+
25+
Input: n = 14
26+
Output: false
27+
Explanation: 14 is not ugly since it includes the prime factor 7.
28+
29+
30+
Constraints:
31+
32+
-231 <= n <= 231 - 1
33+
34+
*/
35+
36+
/*
37+
38+
39+
40+
*/
41+
42+
class A {
43+
// Runtime: 546 ms, faster than 100.00% of Dart online submissions for Ugly Number.
44+
// Memory Usage: 143.5 MB, less than 100.00% of Dart online submissions for Ugly Number.
45+
46+
bool isUgly(int n) {
47+
if (n == 1)
48+
return true;
49+
else if (n <= 0) return false;
50+
if (n % 2 == 0) return isUgly(n ~/ 2);
51+
if (n % 3 == 0) return isUgly(n ~/ 3);
52+
if (n % 5 == 0) return isUgly(n ~/ 5);
53+
return false;
54+
}
55+
}
56+
57+
class B {
58+
bool isUgly(int n) {
59+
while (n > 1) {
60+
if (n >= 5 && n % 5 == 0) {
61+
n = (n ~/ 5);
62+
} else if (n >= 3 && n % 3 == 0) {
63+
n = (n ~/ 3);
64+
} else if (n % 2 == 0) {
65+
n = (n ~/ 2);
66+
} else
67+
return false;
68+
}
69+
if (n == 1) {
70+
return true;
71+
}
72+
73+
if (n <= 0) {
74+
return false;
75+
}
76+
return true;
77+
}
78+
}
79+
80+
class C {
81+
bool isUgly(int n) {
82+
if (n < 1) return false;
83+
84+
while (n % 2 == 0 || n % 3 == 0 || n % 5 == 0) {
85+
if (n % 2 == 0)
86+
n ~/= 2;
87+
else if (n % 3 == 0)
88+
n ~/= 3;
89+
else
90+
n ~/= 5;
91+
}
92+
93+
if (n == 1) return true;
94+
return false;
95+
}
96+
}
97+
98+
class D {
99+
bool isUgly(int n) {
100+
if (n <= 0) return false;
101+
if (n == 1) return true;
102+
103+
while (n % 5 == 0 && n >= 5) n ~/= 5;
104+
105+
while (n % 3 == 0 && n >= 3) n ~/= 3;
106+
107+
while (n % 2 == 0 && n >= 2) n ~/= 2;
108+
109+
// if(n==0 || n==1|| n==2 || n==3 || n==5) return true;
110+
if (n == 1)
111+
return true;
112+
else
113+
return false;
114+
}
115+
}
116+
117+
class E {
118+
// Runtime: 513 ms, faster than 100.00% of Dart online submissions for Ugly Number.
119+
// Memory Usage: 143.2 MB, less than 100.00% of Dart online submissions for Ugly Number.
120+
bool isUgly(int n) {
121+
List<int> arr = [2, 3, 5];
122+
for (int i = 0; i < arr.length; i++) {
123+
while (n >= arr[i] && n % arr[i] == 0) {
124+
n ~/= arr[i];
125+
}
126+
}
127+
return n == 1;
128+
}
129+
}

UglyNumber/ugly_number.go

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package main
2+
3+
// Runtime: 0 ms, faster than 100.00% of Go online submissions for Ugly Number.
4+
// Memory Usage: 2.1 MB, less than 5.43% of Go online submissions for Ugly Number.
5+
func isUgly(n int) bool {
6+
if n == 1 {
7+
return true
8+
} else if n <= 0 {
9+
return false
10+
}
11+
12+
if n%2 == 0 {
13+
return isUgly(n / 2)
14+
}
15+
if n%3 == 0 {
16+
return isUgly(n / 3)
17+
}
18+
if n%5 == 0 {
19+
return isUgly(n / 5)
20+
}
21+
return false
22+
}
23+
24+
func isReallyUgly(n int) bool {
25+
var arr []int = []int{2, 3, 5}
26+
for i := 0; i < len(arr); i++ {
27+
for n >= arr[i] && n%arr[i] == 0 {
28+
n /= arr[i]
29+
}
30+
}
31+
return n == 1
32+
}
33+
34+
func isReallyReallyUgly(n int) bool {
35+
if n <= 0 {
36+
return false
37+
}
38+
if n == 1 {
39+
return true
40+
}
41+
42+
for n%3 == 0 && n >= 3 {
43+
n /= 3
44+
}
45+
for n%5 == 0 && n >= 5 {
46+
n /= 5
47+
}
48+
for n%2 == 0 && n >= 2 {
49+
n /= 2
50+
}
51+
52+
// if(n==0 || n==1|| n==2 || n==3 || n==5) return true;
53+
if n == 1 {
54+
return true
55+
}
56+
return false
57+
58+
}

UglyNumber/ugly_number.md

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# 🔥 Ugly Number 🔥 || 5 Solutions || Simple Fast and Easy || with Explanation
2+
3+
## Approach
4+
5+
- Loop for all positive integers till the ugly number count is smaller than n, if an integer is ugly than increment ugly number count.
6+
- To check if a number is ugly, divide the number by greatest divisible powers of 2, 3 and 5, if the number becomes 1 then it is an ugly number otherwise not.
7+
8+
- For example, let us see how to check for 300 is ugly or not. Greatest divisible power of 2 is 4, after dividing 300 by 4 we get 75. Greatest divisible power of 3 is 3, after dividing 75 by 3 we get 25. Greatest divisible power of 5 is 25, after dividing 25 by 25 we get 1. Since we get 1 finally, 300 is ugly number.
9+
10+
## Solution - 1
11+
12+
```dart
13+
class Solution {
14+
// Runtime: 546 ms, faster than 100.00% of Dart online submissions for Ugly Number.
15+
// Memory Usage: 143.5 MB, less than 100.00% of Dart online submissions for Ugly Number.
16+
17+
bool isUgly(int n) {
18+
if (n == 1)
19+
return true;
20+
else if (n <= 0) return false;
21+
if (n % 2 == 0) return isUgly(n ~/ 2);
22+
if (n % 3 == 0) return isUgly(n ~/ 3);
23+
if (n % 5 == 0) return isUgly(n ~/ 5);
24+
return false;
25+
}
26+
}
27+
```
28+
29+
## Solution - 2
30+
31+
```dart
32+
class Solution {
33+
bool isUgly(int n) {
34+
while (n > 1) {
35+
if (n >= 5 && n % 5 == 0) {
36+
n = (n ~/ 5);
37+
} else if (n >= 3 && n % 3 == 0) {
38+
n = (n ~/ 3);
39+
} else if (n % 2 == 0) {
40+
n = (n ~/ 2);
41+
} else
42+
return false;
43+
}
44+
if (n == 1) {
45+
return true;
46+
}
47+
48+
if (n <= 0) {
49+
return false;
50+
}
51+
return true;
52+
}
53+
}
54+
```
55+
56+
## Solution - 3
57+
58+
```dart
59+
class Solution {
60+
bool isUgly(int n) {
61+
if (n < 1) return false;
62+
63+
while (n % 2 == 0 || n % 3 == 0 || n % 5 == 0) {
64+
if (n % 2 == 0)
65+
n ~/= 2;
66+
else if (n % 3 == 0)
67+
n ~/= 3;
68+
else
69+
n ~/= 5;
70+
}
71+
72+
if (n == 1) return true;
73+
return false;
74+
}
75+
}
76+
```
77+
78+
## Solution - 4
79+
80+
```dart
81+
class Solution {
82+
bool isUgly(int n) {
83+
if (n <= 0) return false;
84+
if (n == 1) return true;
85+
86+
while (n % 5 == 0 && n >= 5) n ~/= 5;
87+
88+
while (n % 3 == 0 && n >= 3) n ~/= 3;
89+
90+
while (n % 2 == 0 && n >= 2) n ~/= 2;
91+
92+
// if(n==0 || n==1|| n==2 || n==3 || n==5) return true;
93+
if (n == 1)
94+
return true;
95+
else
96+
return false;
97+
}
98+
}
99+
```
100+
101+
## Solution - 5
102+
103+
```dart
104+
class Solution {
105+
// Runtime: 513 ms, faster than 100.00% of Dart online submissions for Ugly Number.
106+
// Memory Usage: 143.2 MB, less than 100.00% of Dart online submissions for Ugly Number.
107+
bool isUgly(int n) {
108+
List<int> arr = [2, 3, 5];
109+
for (int i = 0; i < arr.length; i++) {
110+
while (n >= arr[i] && n % arr[i] == 0) {
111+
n ~/= arr[i];
112+
}
113+
}
114+
return n == 1;
115+
}
116+
}
117+
```

0 commit comments

Comments
 (0)