Skip to content
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

Prob-1 & 2 added #28

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions ImplementHashset.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
55 changes: 55 additions & 0 deletions ImplementHashset_v2.0.java
Original file line number Diff line number Diff line change
@@ -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<Integer>[] 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
}
}
55 changes: 55 additions & 0 deletions QueueUsingStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
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 all methods
Space Complexity: O(n) where n is number of elements pushed in stack
LC verified
*/

class MyQueue {

Stack<Integer> mainStack = new Stack<>(); //main stack to store elements
Stack<Integer> revStack = new Stack<>(); //stack for reversing the elements while getting first element in mainStack

public MyQueue() {

}

/** Push element x to the back of queue. */
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

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

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() && revStack.empty()); //return true if stack is empty
}
}