最后两个点MLE,求救!!
查看原帖
最后两个点MLE,求救!!
1321108
GoBlock2021楼主2024/4/11 09:44

最后两个点MLE了,求救!

光想着降时间复杂度了,好像还没法改,有没有大佬救救

ans_list = []
ans = 0
temp = {}

def son(s1,s2,s3):
    
    # 因为要当索引用所以变成元组处理
    s1,s2,s3 = tuple(s1),tuple(s2),tuple(s3)
    global temp
    global ans_list
    global ans
    
    # 查缓存,如果已经考虑过了就不计入答案,如果没有,标记成已考虑
    if temp.get( (s1,s2,s3), None) == 1:
        return
    else:
        temp[(s1,s2,s3)] = 1

    # 如果初始的数组和栈都是空的,说明已经是最终状态了
    if not s1 and not s2:
        ans = ans + 1
        ans_list.append(s1)
        
    # 如果初始的数组不是空的
    if s1:
        # 压栈
        son(s1[:-1], s2 + (s1[-1],), s3)
    # 如果栈不是空的
    if s2:
        # 弹栈
        son(s1, s2[:-1], s3 + (s2[-1],))

userInput = tuple(range(1,int(input())+1))    
son(userInput, tuple(), tuple())
print(ans)
2024/4/11 09:44
加载中...