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 1 commit
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;
}
}
60 changes: 60 additions & 0 deletions QueueUsingStack.java
Original file line number Diff line number Diff line change
@@ -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<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
/** 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
}
}