-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode_638.py
More file actions
41 lines (39 loc) · 1.25 KB
/
LeetCode_638.py
File metadata and controls
41 lines (39 loc) · 1.25 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class Solution(object):
def shoppingOffers(self, price, special, needs):
"""
:type price: List[int]
:type special: List[List[int]]
:type needs: List[int]
:rtype: int
"""
# price, special, needs
# [2,3,4], [[1,1,0,4],[2,2,1,9]], [1,2,1]
memo = {0: 0}
def needs2num(n):
x = 0
for num in n:
x = x * 10 + num
return x
def dfs(dfs_needs):
num = needs2num(dfs_needs)
if num in memo:
return memo[num]
cost = 0
for i in range(len(dfs_needs)):
cost += dfs_needs[i] * price[i]
for s in special:
if s[-1] >= cost:
continue
next_need = []
for i in range(len(dfs_needs)):
ans = dfs_needs[i] - s[i]
if ans < 0:
next_need = []
break
next_need.append(ans)
if len(next_need) == 0:
continue
cost = min(cost, s[-1] + dfs(next_need))
memo[num] = cost
return cost
return dfs(needs)