拼多多2:
class solution():
def judge(self,s):
self.res = False
self.dfs(s,'')
print(self.res)
def dfs(self,s,visited):
if self.res:
return
if not s and visited[0]==visited[-1]:
self.res = True
return
for i,e in enumerate(s):
if not visited:
self.dfs(s[:i] + s[i + 1:], e[0]+e[-1])
elif e[0] == visited[-1]:
self.dfs(s[:i]+s[i+1:],visited[0]+e[-1])
s = ['AB','BA','CC']
t = solution()
t.judge(s)
拼多多3:
import heapq
a = [
[5, 6],
[1, 2, 1, 1, 1],
[1, 2],
[1, 3],
[1, 4],
[2, 5],
[3, 5],
[4, 5]
]
m = a[0][0]
n = a[0][1]
task = a[1]
relationship = a[2:]
def update(index,dic):
for key in list(dic):
if index in dic[key]:
dic[key].remove(index)
if len(dic[key])==0:
heapq.heappush(heaps, [task[key-1],key])
del dic[key]
dic = {}
temp = []
heaps = []
sol = []
for i in relationship: #建立依赖关系字典
if not i[1] in dic:
dic[i[1]] = [i[0]]
else:
dic[i[1]].append(i[0])
for x in range(m):
if x+1 not in dic:
heapq.heappush(heaps,[task[x],x+1]) #初始化小顶堆
while len(sol)<m:
index = heapq.heappop(heaps)
sol.append(index[1])
update(index[1],dic) #更新依赖关系,如果发现长度为0,,加入小顶堆,然后删除
print(sol)
拼多多4:
n = 10
l = [1,2,3,4,5,6,7,8,9,10]
w = [1,1,1,1,1,1,1,1,2,10]
dp = [[float('inf')]*n for i in range(n)]
Z = zip(l,w)
Z = sorted(Z)
l, w = zip(*Z)
dp[0][0] = w[0]
for x in range(1,n):
dp[x][0]=w[x]
sol = 0
for i in range(1,n):
for j in range(1,n):
if j >i:
continue
temp = float('inf')#j不会大于i
for k in range(i):
if dp[k][j-1]<temp:
temp = dp[k][j-1]
if temp<=7*w[i]:
dp[i][j] = temp+w[i]
if j>sol:
sol = j
print(sol+1)