Skip to content

Commit 0283801

Browse files
committed
Runtime: 2371 ms (Top 59.7%) | Memory: 646.15 MB (Top 20.7%)
1 parent 981c9fd commit 0283801

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Runtime: 2371 ms (Top 59.7%) | Memory: 646.15 MB (Top 20.7%)
2+
3+
class Solution:
4+
MOD = 1_000_000_007
5+
6+
def numberOfWays(self, num: int, power: int) -> int:
7+
@cache
8+
def calc_ways(i: int, left: int) -> int:
9+
if left == 0:
10+
return 1
11+
elif left < 0 or i >= len(candidates) or candidates[i] > left:
12+
return 0
13+
else:
14+
return (
15+
calc_ways(i + 1, left) + calc_ways(i + 1, left - candidates[i])
16+
) % self.MOD
17+
18+
candidates = list(self.__get_candidates(num, power))
19+
return calc_ways(0, num)
20+
21+
@staticmethod
22+
def __get_candidates(n: int, p: int) -> Generator:
23+
for num in map(lambda x: x ** p, range(1, n + 1)):
24+
if num <= n:
25+
yield num
26+
else:
27+
break

0 commit comments

Comments
 (0)