Reputation: 63
Following is the code, when I run solution.shoppingOffers([2,5], [[3,0,5],[1,2,10]], [3,2]), python shows: RecursionError: maximum recursion depth exceeded in comparison.
class Solution(object):
def shoppingOffers(self, price, special, needs):
"""
:type price: List[int]
:type special: List[List[int]]
:type needs: List[int]
:rtype: int
"""
return self.helper(price, special, needs, 0)
def helper(self, price, special, needs, index):
if index == len(special):
return self.dot_product(price, needs)
else:
offer = special[index]
flag = False
for index, value in enumerate(needs):
if value < offer[index]:
flag = True
break
if flag:
return self.helper(price, special, needs, index + 1)
else:
return min(self.helper(price, special, needs, index + 1), offer[-1] + self.helper(price, special, self.minus(needs, offer), index))
def dot_product(self, prices, needs):
return sum(i[0] * i[1] for i in zip(prices, needs))
def minus(self, needs, offer):
return [i[0] - i[1] for i in zip(needs, offer)]
Upvotes: 1
Views: 273
Reputation: 127
I don't know the background of the programm but is it correct that you want to overwrite the index variable in the for loop?
for index, value in enumerate(needs):
if value < offer[index]:
flag = True
break
That way the recursion might go on infinitely since
if index == len(special):
return self.dot_product(price, needs)
never holds True. In that case changing the variable name might fix the problem.
Upvotes: 1