Skip to content

Commit 38d642e

Browse files
add 970
1 parent 8b97c7b commit 38d642e

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Your ideas/fixes/algorithms are more than welcome!
2929

3030
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
3131
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
32+
|970|[Powerful Integers](https://leetcode.com/problems/powerful-integers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_970.java) | O(?) | O(1) | |Easy| Math
3233
|966|[Vowel Spellchecker](https://leetcode.com/problems/vowel-spellchecker/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_966.java) | O(hlogn) | O(n) | |Medium| Hash Table, String
3334
|965|[Univalued Binary Tree](https://leetcode.com/problems/univalued-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_965.java) | O(n) | O(h) | |Easy| DFS, recursion|
3435
|961|[N-Repeated Element in Size 2N Array](https://leetcode.com/problems/n-repeated-element-in-size-2n-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_961.java) | O(n) | O(1) | |Easy|
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.HashSet;
6+
import java.util.List;
7+
import java.util.Set;
8+
9+
/**
10+
* 970. Powerful Integers
11+
*
12+
* Given two non-negative integers x and y, an integer is powerful if it is equal to x^i + y^j for some integers i >= 0 and j >= 0.
13+
*
14+
* Return a list of all powerful integers that have value less than or equal to bound.
15+
*
16+
* You may return the answer in any order. In your answer, each value should occur at most once.
17+
*
18+
* Example 1:
19+
*
20+
* Input: x = 2, y = 3, bound = 10
21+
* Output: [2,3,4,5,7,9,10]
22+
* Explanation:
23+
* 2 = 2^0 + 3^0
24+
* 3 = 2^1 + 3^0
25+
* 4 = 2^0 + 3^1
26+
* 5 = 2^1 + 3^1
27+
* 7 = 2^2 + 3^1
28+
* 9 = 2^3 + 3^0
29+
* 10 = 2^0 + 3^2
30+
*
31+
* Example 2:
32+
*
33+
* Input: x = 3, y = 5, bound = 15
34+
* Output: [2,4,6,8,10,14]
35+
*
36+
*
37+
* Note:
38+
* 1 <= x <= 100
39+
* 1 <= y <= 100
40+
* 0 <= bound <= 10^6
41+
*/
42+
public class _970 {
43+
public static class Solution1 {
44+
/**This approach results in Time Limit Exceeded since it's apparently doing
45+
* redundant checks.*/
46+
public List<Integer> powerfulIntegers(int x, int y, int bound) {
47+
Set<Integer> result = new HashSet<>();
48+
int small = x;
49+
int big = y;
50+
if (x > y) {
51+
small = y;
52+
big = x;
53+
}
54+
int maxPower = bound / small;
55+
for (int i = 0; i <= maxPower+1; i++) {
56+
for (int j = 0; j <= maxPower+1; j++) {
57+
int sum = (int) (Math.pow(small, i) + Math.pow(big, j));
58+
if (sum <= bound) {
59+
result.add(sum);
60+
}
61+
}
62+
}
63+
List<Integer> list = new ArrayList<>(result);
64+
Collections.sort(list);
65+
return list;
66+
}
67+
}
68+
69+
public static class Solution2 {
70+
/** credit: https://leetcode.com/problems/powerful-integers/discuss/214212/JavaC%2B%2BPython-Brute-Force */
71+
public List<Integer> powerfulIntegers(int x, int y, int bound) {
72+
Set<Integer> result = new HashSet<>();
73+
for (int i = 1; i < bound; i *= x > 1 ? x : bound + 1) {
74+
for (int j = 1; i + j <= bound; j *= y > 1 ? y : bound + 1) {
75+
result.add(i + j);
76+
}
77+
}
78+
return new ArrayList<>(result);
79+
}
80+
}
81+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._970;
4+
import java.util.Arrays;
5+
import java.util.Collections;
6+
import java.util.List;
7+
import org.junit.BeforeClass;
8+
import org.junit.Test;
9+
10+
import static org.junit.Assert.assertEquals;
11+
12+
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+
}
55+
}

0 commit comments

Comments
 (0)