Leetcode周赛 Weekly Contest 132

1025. Divisor Game (easy)

除数游戏,我的思路是模拟整个游戏的过程,不断更新N值直到0为止。

    def divisorGame(self, N: int) -> bool:
        res = 0
        while N:
            flag = True
            for x in range(1,N):
                if N % x == 0:
                    N = N - x
                    flag = False
                    res += 1
                    break
            if flag:
                break
        return True if res % 2 else False

看了discuss后,发现自己傻逼了。。。
每次都是除以1 相当于都是每次N-1,因此规律就可以推出来了。

return N % 2 == 0

1026. Maximum Difference Between Node and Ancestor (Medium)

BFS每次存好最大值和最小值即可。

    def maxAncestorDiff(self, root: TreeNode) -> int:
        if not root:
            return None
        Min = Max = root.val
        res = 0
        queue = [(root,Min,Max)]
        while queue:
            tmp = []
            for node,Min,Max in queue:
                res = max(abs(node.val-Min),abs(Max-node.val),res)
                Min,Max = min(node.val,Min),max(node.val,Max)
                if node.left:
                    tmp.append((node.left,Min,Max))
                if node.right:
                    tmp.append((node.right,Min,Max))
            queue = tmp
        return res

1027. Longest Arithmetic Sequence (Medium)

该题暴力O(n^3) c++可以过,python超时TLE。

    def longestArithSeqLength(self, A: List[int]) -> int:
        #O(n^3) python超时
        '''
        if len(A) <= 1:
            return 0
        res = 0
        n = len(A)
        for i in range(n-2):
            for j in range(i+1,n-1):
                value = A[j] - A[i]
                num = A[j]
                s = 2
                for k in range(j+1,n):
                    if A[k] - num == value:
                        num = A[k]
                        s += 1
                res = max(res,s)
        return res

DP动态规划,空间换时间 dp[i][diff] 代表i位置差值为diff的Longest Arithmetic Sequence i>j且i-j之间没有差值为diff的点 则

dp[i][diff] = max(dp[i][diff], 1 + dp[j][diff])
    def longestArithSeqLength(self, A: List[int]) -> int:
        #O(n^2) DP
        n = len(A)
        if n <= 2:
            return n
        res = 1
        dp = [collections.defaultdict(int) for _ in range(n)]
        for i in range(1, n):
            for j in range(i-1,-1,-1):
                diff = A[i] - A[j]
                dp[i][diff] = max(dp[i][diff], 1 + dp[j][diff])
                res = max(res, dp[i][diff])
        return res+1

1028. Recover a Tree From Preorder Traversal (Hard)

前序遍历,根据特点 按顺序会一开始遍历到最左下边的叶子节点,没做出来,看了下discuss mark一下。

    def recoverFromPreorder(self, S: str) -> TreeNode:
        stack, i = [], 0
        while i < len(S):
            level, value = 0, ""
            while i < len(S) and S[i] == "-": #记录当前节点深度
                level, i = level + 1, i + 1
            while i < len(S) and S[i] != "-": #记录当前节点值value
                value, i = value + S[i], i + 1
            while len(stack) > level:
                stack.pop()
            node = TreeNode(int(value))
            if stack and not stack[-1].left:
                stack[-1].left = node
            elif stack and not stack[-1].right:
                stack[-1].right = node
            stack.append(node)
        return stack[0]

你可能感兴趣的:(Leetcode周赛 Weekly Contest 132)