-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
please review #34
Open
PrathushaKoouri
wants to merge
1
commit into
super30admin:master
Choose a base branch
from
PrathushaKoouri:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
please review #34
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# used 2 stacks to implement queue. | ||
# before performing pop and peek check move all the items to outstack if that is empty. | ||
# time complexity for push and isempty - O(1) | ||
# time complexity for pop and peek - O(N) | ||
class MyQueue: | ||
|
||
def __init__(self): | ||
""" | ||
Initialize your data structure here. | ||
""" | ||
self.inStack = [] # initializing python list as instack | ||
self.outStack = [] # initializing python list as outstack | ||
|
||
def push(self, x: int) -> None: | ||
""" | ||
Push element x to the back of queue. | ||
""" | ||
|
||
self.inStack.append(x) # push new items to instack | ||
# print(self.inStack, self.outStack) | ||
|
||
def pop(self) -> int: | ||
""" | ||
Removes the element from in front of queue and returns that element. | ||
""" | ||
self.moveItems() # calling move method. | ||
return self.outStack.pop() # pop peek(top) item from outstack | ||
|
||
def peek(self) -> int: | ||
""" | ||
Get the front element. | ||
""" | ||
self.moveItems() # calling move method. | ||
return self.outStack[-1] # extracting top element i.e., last inserted item to the outstack. | ||
|
||
def empty(self) -> bool: | ||
""" | ||
Returns whether the queue is empty. | ||
""" | ||
if not self.inStack and not self.outStack: | ||
return True # if both stacks are empty return true. | ||
|
||
def moveItems(self): | ||
if not self.outStack: # if outstack is empty then push all items from instack to outstack | ||
while self.inStack: | ||
self.outStack.append(self.inStack.pop()) # pop one item at a time from instack and push to outstack. |
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# As per the class I tried double hashing technique for hashset, in leetcode failed 4 testcases. Not sure. | ||
# My challenges were Initializing lists with boolean variables, and accessing public variables where i was stuck. | ||
class MyHashSet: | ||
|
||
def __init__(self): | ||
""" | ||
Initialize your data structure here. | ||
""" | ||
self.buckets = 1000 # list of 1000 items | ||
self.bucketItems = 1000 # list of 1000 items | ||
self.storage = [None for i in range(self.buckets)] # initializing list variables as None at beginning | ||
|
||
def bucketsHash(self, value): | ||
return value % self.buckets #return hash code | ||
|
||
def bucketItemsHash(self, value): | ||
return value % self.bucketItems # return hashcode | ||
|
||
def add(self, value: int) -> None: | ||
buc = self.bucketsHash(value) # get the hashcode for main list | ||
bucItem = self.bucketItemsHash(value) # get the hash code for sublist | ||
# print(buc,bucItem) | ||
if self.storage[buc] is None: | ||
self.storage[buc] = [None for i in range(self.bucketItems)] # if main list is none initialize sublist | ||
if self.storage[buc] is not None and self.storage[buc][bucItem] is None: | ||
# if main list is initialized and having a sublist and sublist hash item | ||
# is None then initialize it to True which adds an item. | ||
Comment on lines
+25
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't need this if |
||
self.storage[buc][bucItem] = True | ||
# print(self.storage[buc][bucItem]) | ||
|
||
def remove(self, value: int) -> None: | ||
buc = self.bucketsHash(value) # get the hashcode for main list | ||
bucItem = self.bucketItemsHash(value) # get the hash code for sublist | ||
if self.storage[buc] is not None: | ||
# if mainlist is not none then make the item false | ||
# to remove the item. | ||
self.storage[buc][bucItem] = False | ||
|
||
def contains(self, value: int) -> bool: | ||
""" | ||
Returns true if this set contains the specified element | ||
""" | ||
buc = self.bucketsHash(value) # get the hashcode for main list | ||
bucItem = self.bucketItemsHash(value) # get the hash code for sublist | ||
if self.storage[buc] is not None and self.storage[buc][bucItem]: | ||
# if main list is not none and sublist is true then return true as element is found | ||
# else return false as the element doesnt exist. | ||
return True | ||
|
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# time complexity for operations is O(1) but the space required is too high. | ||
|
||
class MyHashSet: | ||
|
||
def __init__(self): | ||
""" | ||
Initialize your data structure here. | ||
""" | ||
self.buckets = 1000000 # initializing 1 million records array | ||
self.storage = [None for i in range(self.buckets)] #initializing a list | ||
|
||
def bucketsHash(self, value): | ||
return value % self.buckets # developing a hash function | ||
|
||
def add(self, value: int) -> None: | ||
buc = self.bucketsHash(value) # getting hash value | ||
# print(buc,bucItem) | ||
# if self.storage[buc] is None: | ||
self.storage[buc] = True # inserting true for the data if exists | ||
# print(self.storage[buc][bucItem]) | ||
|
||
def remove(self, value: int) -> None: | ||
buc = self.bucketsHash(value) # getting hash value | ||
if self.storage[buc] is not None: | ||
self.storage[buc] = False # if the value is present remove it by making false | ||
|
||
def contains(self, value: int) -> bool: | ||
""" | ||
Returns true if this set contains the specified element | ||
""" | ||
buc = self.bucketsHash(value) # getting hash value | ||
if self.storage[buc] == True: | ||
return True # if the value is present return true. |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
value // bucketItems