forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8221bdb
commit cc18380
Showing
1 changed file
with
8 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |