Skip to content

Commit

Permalink
Refine Python solutions again
Browse files Browse the repository at this point in the history
  • Loading branch information
huangsam committed Nov 11, 2024
1 parent bcee582 commit 953c8fa
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 25 deletions.
8 changes: 5 additions & 3 deletions python/deleteNode.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# https://leetcode.com/problems/delete-node-in-a-bst/

from typing import Optional

from container.binary_tree import TreeNode


class Solution:
def deleteNode(self, root: TreeNode, key: int) -> TreeNode:
def deleteNode(self, root: Optional[TreeNode], key: int) -> Optional[TreeNode]:
if root is None:
return None
if key < root.val:
Expand All @@ -16,8 +18,8 @@ def deleteNode(self, root: TreeNode, key: int) -> TreeNode:
return root.right
elif root.right is None:
return root.left
minNode = self.findMin(root.right)
root.val = minNode.val
min_node = self.findMin(root.right)
root.val = min_node.val
root.right = self.deleteNode(root.right, root.val)
return root

Expand Down
4 changes: 3 additions & 1 deletion python/invertTree.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# https://leetcode.com/problems/invert-binary-tree/

from typing import Optional

from container.binary_tree import TreeNode


class Solution:
def invertTree(self, root: TreeNode) -> TreeNode:
def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
if root is None:
return None
if root.left or root.right:
Expand Down
6 changes: 4 additions & 2 deletions python/isBalanced.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# https://leetcode.com/problems/balanced-binary-tree/

from typing import Optional

from container.binary_tree import TreeNode


class Solution:
def isBalanced(self, root: TreeNode) -> bool:
def isBalanced(self, root: Optional[TreeNode]) -> bool:
return self.nodeHeight(root) != -1

def nodeHeight(self, root: TreeNode) -> int:
def nodeHeight(self, root: Optional[TreeNode]) -> int:
if root is None:
return 0
left_height = self.nodeHeight(root.left)
Expand Down
16 changes: 8 additions & 8 deletions python/jumpGame.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ def canJumpDynamic(self, nums: List[int]) -> bool:
Approach (sub-efficient):
- Initialize reachable array
- Base case is reachable[0] = True
- For each position i, mark i..i+nval as reachable
- Check if nlen-1 is reachable
- For each position i, mark i...i+n_val as reachable
- Check if n_len-1 is reachable
"""
if len(nums) <= 1:
return True
nlen = len(nums)
reachable = [False] * nlen
n_len = len(nums)
reachable = [False] * n_len
reachable[0] = True
for nidx, nval in enumerate(nums):
if reachable[nidx] is False:
for n_idx, n_val in enumerate(nums):
if reachable[n_idx] is False:
continue
for i in range(nidx + 1, min(nidx + nval + 1, nlen)):
for i in range(n_idx + 1, min(n_idx + n_val + 1, n_len)):
reachable[i] = True
return reachable[nlen - 1]
return reachable[n_len - 1]
8 changes: 4 additions & 4 deletions python/modifiedList.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ def modifiedList(self, nums: List[int], head: Optional[ListNode]) -> Optional[Li
prev = None

while current is not None:
next = current.next
tmp = current.next

# Should be an O(1) check given that num_set is bounded
# to 10^5
if current.val in num_set:
# Case 1: Current is the first node
if first is current:
first = next
first = tmp

# Case 2: Current is not the first node
elif prev is not None:
prev.next = next
prev.next = tmp

# Current is cleared away and prev remains. So prev
# pointer does not need to change at all
Expand All @@ -37,7 +37,7 @@ def modifiedList(self, nums: List[int], head: Optional[ListNode]) -> Optional[Li
# at this node as we iterate through the OG list
prev = current

current = next
current = tmp

# We ultimately want the head of the modified linked list
return first
1 change: 0 additions & 1 deletion python/removeNthFromEnd.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
while curr_node is not None:
node_list.append(curr_node)
curr_node = curr_node.next
node_to_delete = None
nlen = len(node_list)
node_to_delete = node_list[nlen - n]
if nlen - n > 0:
Expand Down
4 changes: 2 additions & 2 deletions python/reverseBetween.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ def reverseBetween(self, head: Optional[ListNode], left: int, right: int) -> Opt
The simplest way is to disconnect the left <= right subset
from the linked list, reverse it and reconnect it back to
the list afterwards. Assuming that left > 1 or right still
has at least one node afterwards.
the list afterward. Assuming that left > 1 or right still
has at least one node afterward.
Keep in mind that left can equal right. So in that case, nothing
needs to be done as it were.
Expand Down
File renamed without changes.
6 changes: 4 additions & 2 deletions python/sortLinkedList.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# https://leetcode.com/problems/sort-list/

from typing import Optional

from container.linked_list import ListNode


class Solution:
def sortList(self, head: ListNode) -> ListNode:
def sortList(self, head: Optional[ListNode]) -> Optional[ListNode]:
if head is None or head.next is None:
return head
middle = self.getMiddle(head)
Expand All @@ -21,7 +23,7 @@ def getMiddle(self, head: ListNode) -> ListNode:
slow = slow.next
return slow

def mergeSortedLists(self, l1: ListNode, l2: ListNode) -> ListNode:
def mergeSortedLists(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
dummy = ListNode()
node = dummy
while l1 and l2:
Expand Down
2 changes: 1 addition & 1 deletion python/uniquePaths.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
class Solution:
def uniquePaths(self, m: int, n: int) -> int:
# initialize memo array
memo = [[0] * n for i in range(m)]
memo = [[0] * n for _ in range(m)]

# go through each row, m-1 --> 0
for r in range(m - 1, -1, -1):
Expand Down
2 changes: 1 addition & 1 deletion python/uniquePaths2.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Solution:
def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int:
# initialize memo array
rows, cols = len(obstacleGrid), len(obstacleGrid[0])
memo = [[0] * cols for i in range(rows)]
memo = [[0] * cols for _ in range(rows)]

# go through each row, m-1 --> 0
for r in range(rows - 1, -1, -1):
Expand Down

0 comments on commit 953c8fa

Please sign in to comment.