From b7bff5a5187921464ead7aeb486eff7c50842adf Mon Sep 17 00:00:00 2001 From: Akhilesh Borgaonkar Date: Tue, 8 Oct 2019 17:21:36 -0700 Subject: [PATCH 1/3] Prob-1 & 2 added --- ImplementHashset.java | 34 ++++++++++++++++++++++++ QueueUsingStack.java | 60 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 ImplementHashset.java create mode 100644 QueueUsingStack.java diff --git a/ImplementHashset.java b/ImplementHashset.java new file mode 100644 index 00000000..11f57e1d --- /dev/null +++ b/ImplementHashset.java @@ -0,0 +1,34 @@ +/* +Author: Akhilesh Borgaonkar +Approach: I am using array to repeat the functionality of hashset. Whenever an element is added in set, I set the key-th index to 1 (viz. by default 0). + Whenever an element is removed, I am setting the key-th index as 0 which means the element is no more contained in the set. + Whenever user wants to check if an element is contained in the set, then I just check the value of key-th index if 1 then contains else missing. +Time Complexity: O(1) for all methods +Space Complexity: O(n) where n is number of elements in array +LC verified +*/ + +class MyHashSet { + + int MAX = 100000; + Integer[] myArray = new Integer[MAX]; //array for storing elements in set + + public MyHashSet() { + Arrays.fill(myArray,0); //set all indexes to 0 + } + + public void add(int key) { + myArray[key] = 1; //setting key-th index as 1 + } + + public void remove(int key) { + myArray[key]=0; //setting key-th index as 0 + } + + /** Returns true if this set contains the specified element */ + public boolean contains(int key) { + if(myArray[key]==1) //if the key-th index is 1 then return true else element is not in array + return true; + return false; + } +} \ No newline at end of file diff --git a/QueueUsingStack.java b/QueueUsingStack.java new file mode 100644 index 00000000..20680492 --- /dev/null +++ b/QueueUsingStack.java @@ -0,0 +1,60 @@ +/* +Author: Akhilesh Borgaonkar +Approach: I am using two stacks here. The main stack maintains the sequence in which elements were pushed in queue at any given time. + Reverse stack is used for poping out the elements from main stack and getting the first element in the main stack. +Time Complexity: O(1) for push() & O(n) for pop() and peek() methods +Space Complexity: O(n) where n is number of elements pushed in stack +LC verified +*/ + +class MyQueue { + + Stack mainStack = new Stack<>(); //main stack to store elements + Stack revStack = new Stack<>(); //stack for reversing the elements while getting first element in mainStack + /** Initialize your data structure here. */ + public MyQueue() { + + } + + /** Push element x to the back of queue. */ + public void push(int x) { + mainStack.push(x); + } + + /** Removes the element from in front of queue and returns that element. */ + public int pop() { + int popElement = -1; //default value if stack is empty + + if(!mainStack.empty()){ + while(!mainStack.empty()) + revStack.push(mainStack.pop()); //getting all elements from mainStack and putting in revStack to reverse the order and get the first element pushed in mainStack + + popElement = revStack.pop(); //poping the first element in queue + + while(!revStack.empty()) //reverting the sequence to mainStack + mainStack.push(revStack.pop()); + } + return popElement; + } + + /** Get the front element. */ + public int peek() { + int peekElement = -1; //default value if stack is empty + + if(!mainStack.empty()){ + while(!mainStack.empty()) + revStack.push(mainStack.pop()); //getting all elements from mainStack and putting in revStack to reverse the order and get the first element pushed in mainStack + + peekElement = revStack.peek(); //peeking the first element in queue w/o poping + + while(!revStack.empty()) + mainStack.push(revStack.pop()); //reverting the sequence to mainStack + } + return peekElement; + } + + /** Returns whether the queue is empty. */ + public boolean empty() { + return mainStack.empty(); //return true if stack is empty + } +} \ No newline at end of file From 737f5e081040bd116d5a57df99689b39b81f9251 Mon Sep 17 00:00:00 2001 From: Akhilesh Borgaonkar Date: Tue, 8 Oct 2019 22:38:09 -0700 Subject: [PATCH 2/3] optimized queue using stacks --- QueueUsingStack.java | 41 ++++++++++++++++++----------------------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/QueueUsingStack.java b/QueueUsingStack.java index 20680492..6b7a9c25 100644 --- a/QueueUsingStack.java +++ b/QueueUsingStack.java @@ -2,7 +2,7 @@ Author: Akhilesh Borgaonkar Approach: I am using two stacks here. The main stack maintains the sequence in which elements were pushed in queue at any given time. Reverse stack is used for poping out the elements from main stack and getting the first element in the main stack. -Time Complexity: O(1) for push() & O(n) for pop() and peek() methods +Time Complexity: O(1) for all methods Space Complexity: O(n) where n is number of elements pushed in stack LC verified */ @@ -11,7 +11,7 @@ class MyQueue { Stack mainStack = new Stack<>(); //main stack to store elements Stack revStack = new Stack<>(); //stack for reversing the elements while getting first element in mainStack - /** Initialize your data structure here. */ + public MyQueue() { } @@ -21,40 +21,35 @@ public void push(int x) { mainStack.push(x); } + private void reverseStack(){ + while(!mainStack.empty()) + revStack.push(mainStack.pop()); //getting all elements from mainStack and putting in revStack to reverse the order and get the first element pushed in mainStack + } + /** Removes the element from in front of queue and returns that element. */ public int pop() { - int popElement = -1; //default value if stack is empty + int popElement = -1; //default value if stack is empty - if(!mainStack.empty()){ - while(!mainStack.empty()) - revStack.push(mainStack.pop()); //getting all elements from mainStack and putting in revStack to reverse the order and get the first element pushed in mainStack - - popElement = revStack.pop(); //poping the first element in queue - - while(!revStack.empty()) //reverting the sequence to mainStack - mainStack.push(revStack.pop()); - } + if(revStack.empty()) + reverseStack(); //populate the reverse stack + + popElement = revStack.pop(); //poping the first element in queue return popElement; } /** Get the front element. */ public int peek() { - int peekElement = -1; //default value if stack is empty + int peekElement = -1; //default value if stack is empty - if(!mainStack.empty()){ - while(!mainStack.empty()) - revStack.push(mainStack.pop()); //getting all elements from mainStack and putting in revStack to reverse the order and get the first element pushed in mainStack - - peekElement = revStack.peek(); //peeking the first element in queue w/o poping - - while(!revStack.empty()) - mainStack.push(revStack.pop()); //reverting the sequence to mainStack - } + if(revStack.empty()) + reverseStack(); //populate the reverse stack + + peekElement = revStack.peek(); //peeking the first element in queue w/o poping return peekElement; } /** Returns whether the queue is empty. */ public boolean empty() { - return mainStack.empty(); //return true if stack is empty + return (mainStack.empty() && revStack.empty()); //return true if stack is empty } } \ No newline at end of file From 4616318f96dfdb24829fe68519137ee116c4c467 Mon Sep 17 00:00:00 2001 From: Akhilesh Borgaonkar Date: Sat, 12 Oct 2019 15:18:41 -0700 Subject: [PATCH 3/3] adding double hashing method --- ImplementHashset_v2.0.java | 55 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 ImplementHashset_v2.0.java diff --git a/ImplementHashset_v2.0.java b/ImplementHashset_v2.0.java new file mode 100644 index 00000000..deba2748 --- /dev/null +++ b/ImplementHashset_v2.0.java @@ -0,0 +1,55 @@ +/* +Author: Akhilesh Borgaonkar +Approach: I have implemented using arrays and double hashing method to avoid collisions. The boolean value is stored at the index key of the sub-array + which is nested in the parent array. I have two hashing functions, one generates index for parent array and another generates index for sub-array. +Time Complexity: O(1) for get(), put() and remove() +Space Complexity: O(n) where n is number of elements in the array. +LC verified +Scope of improvement: Going to try to use Arraylist[] to reduce space. +*/ + +class MyHashSet { + + int buckets = 1000; + int bucketItems = 1000; + boolean[][] myHashset = new boolean[buckets][]; //my hashset to store boolean values to determine presence of integer in the hashset + + /** Initialize your data structure here. */ + public MyHashSet() { + + } + + private int getBucketIdx(int value){ + return value%buckets; //hashing function-1 to get index of parent array + } + + private int getBucketItemIdx(int value){ + return value/buckets; //hashing function-2 to get index of sub array + } + + public void add(int key) { + int bucket = getBucketIdx(key); //getting the index of parent array + int bucketList = getBucketItemIdx(key); //getting the index of sub array + + if(myHashset[bucket] == null) + myHashset[bucket] = new boolean[bucketItems]; //if the sub-array is never created then create it + + myHashset[bucket][bucketList] = true; //marking the presence of this key in my hashset + } + + public void remove(int key) { + int bucket = getBucketIdx(key); + int bucketList = getBucketItemIdx(key); + + if(myHashset[bucket] != null) + myHashset[bucket][bucketList] = false; //marking the location as false to state that this key is no more present + } + + /** Returns true if this set contains the specified element */ + public boolean contains(int key) { + int bucket = getBucketIdx(key); + int bucketList = getBucketItemIdx(key); + + return (myHashset[bucket] != null && myHashset[bucket][bucketList]); //if the sub array is present and is true then key is present in my hashset + } +} \ No newline at end of file