Skip to content

Add Python implementations for LeetCode 232 and 706#2440

Open
rishigoswamy wants to merge 1 commit intosuper30admin:masterfrom
rishigoswamy:master
Open

Add Python implementations for LeetCode 232 and 706#2440
rishigoswamy wants to merge 1 commit intosuper30admin:masterfrom
rishigoswamy:master

Conversation

@rishigoswamy
Copy link

No description provided.

@super30admin
Copy link
Owner

For leetcode232.py (Queue using Stacks):

  • The solution is correct and follows the standard approach.
  • The code is clean and well-commented.
  • One minor improvement: in the pop method, you can call peek to ensure the outputStack has elements, which might make the code slightly more concise (as in the reference solution). However, your current implementation is also correct and clear.

For leetcode706.py (Hash Map):

  • The solution is correct and uses separate chaining with linked lists.
  • In the put method, the variable presentInLinkedList is unnecessary. You can traverse the linked list and update the value if the key exists. If you reach the end without finding the key, you add a new node. This avoids using a flag variable.
  • Example improvement for put:
    def put(self, key: int, value: int) -> None:
        idx = self.hashFunction(key)
        curr = self.array[idx]
        while curr.next:
            if curr.next.key == key:
                curr.next.value = value
                return
            curr = curr.next
        curr.next = Node(key, value)
    
  • However, note that your current implementation uses a dummy node at each bucket, which is good. The above suggestion is just to simplify the code.
  • Also, in the remove method, you are starting from the dummy node and checking nxt.key, which is correct. But ensure that you are not missing the case where the key might be in the dummy node? Actually, the dummy node has key None, so it won't match. So it's safe.

Overall, both solutions are excellent. The code is well-organized and easy to understand.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants