Skip to content

Commit 9a91403

Browse files
refactor 970
1 parent d394591 commit 9a91403

File tree

1 file changed

+37
-66
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+37
-66
lines changed

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

Lines changed: 37 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -6,76 +6,47 @@
66
import java.util.List;
77
import java.util.Set;
88

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-
*/
429
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-
}
10+
public static class Solution1 {
11+
/**
12+
* This approach results in Time Limit Exceeded since it's apparently doing
13+
* redundant checks.
14+
*/
15+
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);
29+
}
30+
}
31+
}
32+
List<Integer> list = new ArrayList<>(result);
33+
Collections.sort(list);
34+
return list;
6135
}
62-
}
63-
List<Integer> list = new ArrayList<>(result);
64-
Collections.sort(list);
65-
return list;
6636
}
67-
}
6837

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);
38+
public static class Solution2 {
39+
/**
40+
* credit: https://leetcode.com/problems/powerful-integers/discuss/214212/JavaC%2B%2BPython-Brute-Force
41+
*/
42+
public List<Integer> powerfulIntegers(int x, int y, int bound) {
43+
Set<Integer> result = new HashSet<>();
44+
for (int i = 1; i < bound; i *= x > 1 ? x : bound + 1) {
45+
for (int j = 1; i + j <= bound; j *= y > 1 ? y : bound + 1) {
46+
result.add(i + j);
47+
}
48+
}
49+
return new ArrayList<>(result);
7650
}
77-
}
78-
return new ArrayList<>(result);
7951
}
80-
}
8152
}

0 commit comments

Comments
 (0)