Skip to content

Commit 0856174

Browse files
add a solution for 264
1 parent 8c56452 commit 0856174

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

src/main/java/com/fishercoder/solutions/_264.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.fishercoder.solutions;
22

3+
import java.util.TreeSet;
4+
35
public class _264 {
46

57
public static class Solution1 {
@@ -31,4 +33,40 @@ public int nthUglyNumber(int n) {
3133
return ugly[n - 1];
3234
}
3335
}
36+
37+
public static class Solution2 {
38+
/**
39+
* My completely original solution on 11/7/2021.
40+
* Although not super robust, as the input increases, I'll have to increase the times (variable n) on line 61 as some smaller numbers might appear later.
41+
*/
42+
public int nthUglyNumber(int n) {
43+
TreeSet<Long> treeSet = new TreeSet<>();
44+
treeSet.add(1l);
45+
int count = 1;
46+
int polled = 0;
47+
int[] primes = new int[]{2, 3, 5};
48+
while (!treeSet.isEmpty()) {
49+
int size = treeSet.size();
50+
for (int i = 0; i < size; i++) {
51+
Long curr = treeSet.pollFirst();
52+
polled++;
53+
if (polled == n) {
54+
return curr.intValue();
55+
}
56+
for (int prime : primes) {
57+
treeSet.add(prime * curr);
58+
count++;
59+
}
60+
}
61+
if (count >= n * 3) {
62+
while (polled < n - 1) {
63+
treeSet.pollFirst();
64+
polled++;
65+
}
66+
return treeSet.pollFirst().intValue();
67+
}
68+
}
69+
return -1;
70+
}
71+
}
3472
}

src/test/java/com/fishercoder/_264Test.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,59 @@
88

99
public class _264Test {
1010
private static _264.Solution1 solution1;
11+
private static _264.Solution2 solution2;
1112

1213
@BeforeClass
1314
public static void setup() {
1415
solution1 = new _264.Solution1();
16+
solution2 = new _264.Solution2();
1517
}
1618

1719
@Test
1820
public void test1() {
1921
assertEquals(12, solution1.nthUglyNumber(10));
22+
assertEquals(12, solution2.nthUglyNumber(10));
2023
}
2124

2225
@Test
2326
public void test2() {
2427
assertEquals(402653184, solution1.nthUglyNumber(1352));
28+
assertEquals(402653184, solution2.nthUglyNumber(1352));
29+
}
30+
31+
@Test
32+
public void test3() {
33+
assertEquals(1, solution1.nthUglyNumber(1));
34+
assertEquals(1, solution2.nthUglyNumber(1));
35+
}
36+
37+
@Test
38+
public void test4() {
39+
assertEquals(2, solution1.nthUglyNumber(2));
40+
assertEquals(2, solution2.nthUglyNumber(2));
41+
}
42+
43+
@Test
44+
public void test5() {
45+
assertEquals(3, solution1.nthUglyNumber(3));
46+
assertEquals(3, solution2.nthUglyNumber(3));
47+
}
48+
49+
@Test
50+
public void test6() {
51+
assertEquals(5, solution1.nthUglyNumber(5));
52+
assertEquals(5, solution2.nthUglyNumber(5));
53+
}
54+
55+
@Test
56+
public void test7() {
57+
assertEquals(3888, solution1.nthUglyNumber(134));
58+
assertEquals(3888, solution2.nthUglyNumber(134));
59+
}
60+
61+
@Test
62+
public void test8() {
63+
assertEquals(536870912, solution1.nthUglyNumber(1407));
64+
assertEquals(536870912, solution2.nthUglyNumber(1407));
2565
}
2666
}

0 commit comments

Comments
 (0)