Skip to content

Commit

Permalink
Update Solution.py
Browse files Browse the repository at this point in the history
  • Loading branch information
CodersAcademy006 authored Sep 14, 2024
1 parent 8221bdb commit cc18380
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions solution/0800-0899/0818.Race Car/Solution.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit cc18380

Please sign in to comment.