Conversation
Owner
|
Strengths:
Areas for Improvement:
Specific Fix for HashSet: def __init__(self):
self.primaryArr = [None] * 1001Then in the add method: def add(self, key: int) -> None:
primaryHash = self.primaryHash(key)
if self.primaryArr[primaryHash] is None:
# Initialize the secondary array for this bucket
self.primaryArr[primaryHash] = [False] * 1000
secondaryHash = self.secondaryHash(key)
self.primaryArr[primaryHash][secondaryHash] = TrueSimilarly, in remove and contains, check for None instead of False: def remove(self, key: int) -> None:
primaryHash = self.primaryHash(key)
secondaryHash = self.secondaryHash(key)
if self.primaryArr[primaryHash] is not None:
self.primaryArr[primaryHash][secondaryHash] = False
def contains(self, key: int) -> bool:
primaryHash = self.primaryHash(key)
secondaryHash = self.secondaryHash(key)
if self.primaryArr[primaryHash] is not None:
return self.primaryArr[primaryHash][secondaryHash]
return False |
Owner
|
For the MyHashSet solution:
Suggested correction for MyHashSet: class MyHashSet:
def __init__(self):
self.primaryArr = [None] * 1001 # Initialize with None
def add(self, key: int) -> None:
primaryHash = self.primaryHash(key)
if self.primaryArr[primaryHash] is None:
# For the first bucket (index 0), we need to handle key=1000000
if primaryHash == 0:
self.primaryArr[primaryHash] = [False] * 1001
else:
self.primaryArr[primaryHash] = [False] * 1000
secondaryHash = self.secondaryHash(key)
self.primaryArr[primaryHash][secondaryHash] = True
def remove(self, key: int) -> None:
primaryHash = self.primaryHash(key)
if self.primaryArr[primaryHash] is None:
return
secondaryHash = self.secondaryHash(key)
self.primaryArr[primaryHash][secondaryHash] = False
def contains(self, key: int) -> bool:
primaryHash = self.primaryHash(key)
if self.primaryArr[primaryHash] is None:
return False
secondaryHash = self.secondaryHash(key)
return self.primaryArr[primaryHash][secondaryHash]
def primaryHash(self, key):
return key // 1000
def secondaryHash(self, key):
return key % 1000For the MinStack solution (optional feedback): It is mostly correct, but you can avoid storing the minItem as a separate variable by always getting it from the top of minOrderedItems. Also, the condition in push should be |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.