diff --git a/solution/0800-0899/0818.Race Car/Solution.py b/solution/0800-0899/0818.Race Car/Solution.py index e1b0a84f19067..dd9413d09bc93 100644 --- a/solution/0800-0899/0818.Race Car/Solution.py +++ b/solution/0800-0899/0818.Race Car/Solution.py @@ -1,12 +1,9 @@ class Solution: - def racecar(self, target: int) -> int: - dp = [0] * (target + 1) - for i in range(1, target + 1): - k = i.bit_length() - if i == 2**k - 1: - dp[i] = k - continue - dp[i] = dp[2**k - 1 - i] + k + 1 - for j in range(k - 1): - dp[i] = min(dp[i], dp[i - (2 ** (k - 1) - 2**j)] + k - 1 + j + 2) - return dp[target] + def pruneTree(self, root): + if not root: + return None + root.left = self.pruneTree(root.left) + root.right = self.pruneTree(root.right) + if not root.left and not root.right and root.val == 0: + return None + return root