forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpowerful-integers.py
More file actions
27 lines (24 loc) · 759 Bytes
/
powerful-integers.py
File metadata and controls
27 lines (24 loc) · 759 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# Time: O((logn)^2), n is the bound
# Space: O(r), r is the size of the result
import math
class Solution(object):
def powerfulIntegers(self, x, y, bound):
"""
:type x: int
:type y: int
:type bound: int
:rtype: List[int]
"""
result = set()
log_x = int(math.floor(math.log(bound) / math.log(x)))+1 if x != 1 else 1
log_y = int(math.floor(math.log(bound) / math.log(y)))+1 if y != 1 else 1
pow_x = 1
for i in xrange(log_x):
pow_y = 1
for j in xrange(log_y):
val = pow_x + pow_y
if val <= bound:
result.add(val)
pow_y *= y
pow_x *= x
return list(result)