Skip to content

Commit 4be926f

Browse files
update a solution for 970
1 parent 19e7a1b commit 4be926f

File tree

2 files changed

+63
-62
lines changed

2 files changed

+63
-62
lines changed

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

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,26 @@
88

99
public class _970 {
1010
public static class Solution1 {
11-
/**
12-
* This approach results in Time Limit Exceeded since it's apparently doing
13-
* redundant checks.
14-
*/
1511
public List<Integer> powerfulIntegers(int x, int y, int bound) {
16-
Set<Integer> result = new HashSet<>();
17-
int small = x;
18-
int big = y;
19-
if (x > y) {
20-
small = y;
21-
big = x;
22-
}
23-
int maxPower = bound / small;
24-
for (int i = 0; i <= maxPower + 1; i++) {
25-
for (int j = 0; j <= maxPower + 1; j++) {
26-
int sum = (int) (Math.pow(small, i) + Math.pow(big, j));
27-
if (sum <= bound) {
28-
result.add(sum);
12+
int powerInteger;
13+
Set<Integer> set = new HashSet<>();
14+
for (int i = 0; i <= bound; i++) {
15+
for (int j = 0; j <= bound; j++) {
16+
powerInteger = (int) (Math.pow(x, i) + Math.pow(y, j));
17+
if (powerInteger <= bound) {
18+
set.add(powerInteger);
19+
} else {
20+
break;
21+
}
22+
if (y == 1) {
23+
break;
2924
}
3025
}
26+
if (x == 1) {
27+
break;
28+
}
3129
}
32-
List<Integer> list = new ArrayList<>(result);
33-
Collections.sort(list);
34-
return list;
30+
return new ArrayList<>(set);
3531
}
3632
}
3733

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,60 @@
11
package com.fishercoder;
22

3+
import com.fishercoder.common.utils.CommonUtils;
34
import com.fishercoder.solutions._970;
5+
46
import java.util.Arrays;
57
import java.util.Collections;
68
import java.util.List;
9+
710
import org.junit.BeforeClass;
811
import org.junit.Test;
912

1013
import static org.junit.Assert.assertEquals;
1114

1215
public class _970Test {
13-
private static _970.Solution1 solution1;
14-
private static _970.Solution2 solution2;
15-
16-
@BeforeClass
17-
public static void setup() {
18-
solution1 = new _970.Solution1();
19-
solution2 = new _970.Solution2();
20-
}
21-
22-
@Test
23-
public void test1() {
24-
assertEquals(Arrays.asList(2, 3, 4, 5, 7, 9, 10), solution1.powerfulIntegers(2, 3, 10));
25-
assertEquals(Arrays.asList(2, 3, 4, 5, 7, 9, 10), solution2.powerfulIntegers(2, 3, 10));
26-
}
27-
28-
@Test
29-
public void test2() {
30-
assertEquals(Arrays.asList(2, 4, 6, 8, 10, 14), solution1.powerfulIntegers(3, 5, 15));
31-
assertEquals(Arrays.asList(2, 4, 6, 8, 10, 14), solution2.powerfulIntegers(3, 5, 15));
32-
}
33-
34-
@Test
35-
public void test3() {
36-
assertEquals(Arrays.asList(2, 3, 5, 7, 8, 9, 10), solution1.powerfulIntegers(2, 6, 12));
37-
assertEquals(Arrays.asList(2, 3, 5, 7, 8, 9, 10), solution2.powerfulIntegers(2, 6, 12));
38-
}
39-
40-
@Test
41-
public void test4() {
42-
assertEquals(Arrays.asList(2, 3, 5, 9, 10, 11), solution1.powerfulIntegers(2, 9, 12));
43-
assertEquals(Arrays.asList(2, 3, 5, 9, 10, 11), solution2.powerfulIntegers(2, 9, 12));
44-
}
45-
46-
@Test
47-
public void test5() {
48-
assertEquals(Arrays.asList(2, 91, 180, 8101, 8190, 16200, 729001, 729090,
49-
737100), solution1.powerfulIntegers(90, 90, 1000000));
50-
List<Integer> actual = solution2.powerfulIntegers(90, 90, 1000000);
51-
Collections.sort(actual);
52-
assertEquals(Arrays.asList(2, 91, 180, 8101, 8190, 16200, 729001, 729090,
53-
737100), actual);
54-
}
16+
private static _970.Solution1 solution1;
17+
private static _970.Solution2 solution2;
18+
19+
@BeforeClass
20+
public static void setup() {
21+
solution1 = new _970.Solution1();
22+
solution2 = new _970.Solution2();
23+
}
24+
25+
@Test
26+
public void test1() {
27+
assertEquals(Arrays.asList(2, 3, 4, 5, 7, 9, 10), solution1.powerfulIntegers(2, 3, 10));
28+
assertEquals(Arrays.asList(2, 3, 4, 5, 7, 9, 10), solution2.powerfulIntegers(2, 3, 10));
29+
}
30+
31+
@Test
32+
public void test2() {
33+
assertEquals(Arrays.asList(2, 4, 6, 8, 10, 14), solution1.powerfulIntegers(3, 5, 15));
34+
assertEquals(Arrays.asList(2, 4, 6, 8, 10, 14), solution2.powerfulIntegers(3, 5, 15));
35+
}
36+
37+
@Test
38+
public void test3() {
39+
assertEquals(Arrays.asList(2, 3, 5, 7, 8, 9, 10), solution1.powerfulIntegers(2, 6, 12));
40+
assertEquals(Arrays.asList(2, 3, 5, 7, 8, 9, 10), solution2.powerfulIntegers(2, 6, 12));
41+
}
42+
43+
@Test
44+
public void test4() {
45+
assertEquals(Arrays.asList(2, 3, 5, 9, 10, 11), solution1.powerfulIntegers(2, 9, 12));
46+
assertEquals(Arrays.asList(2, 3, 5, 9, 10, 11), solution2.powerfulIntegers(2, 9, 12));
47+
}
48+
49+
@Test
50+
public void test5() {
51+
CommonUtils.printList(solution1.powerfulIntegers(90, 90, 1000000));
52+
CommonUtils.printList(solution2.powerfulIntegers(90, 90, 1000000));
53+
}
54+
55+
@Test
56+
public void test6() {
57+
assertEquals(Arrays.asList(2), solution1.powerfulIntegers(1, 1, 40000));
58+
assertEquals(Arrays.asList(2), solution2.powerfulIntegers(1, 1, 40000));
59+
}
5560
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy