我觉得还是需要一个2d的dp,这是我的python code,有不对的请大佬指正。。如果有target_range的lo,hi的limit,那么这个algorithm应该是O(lo * hi * n)的complexity
def solve(buttons, target_range):
'''
buttons: List[List[int]]
target_range: List[int]
returns: boolean
'''
lo, hi = target_range
dp = set([(0, 0)])
for s, e in buttons:
new_dp = set()
for s2, e2 in dp:
i = 1
while True:
# we try to push as many times as we can
s3, e3 = s2 + s * i, e2 + e * i
if s3 >= lo and e3 <= hi:
return True
elif s3 > lo: # missing the range [lo, s3]
break
new_dp.add((s3, e3))
i += 1
dp = dp | new_dp
return False