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

both solutions added #37

Open
wants to merge 2 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
94 changes: 94 additions & 0 deletions HashSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
The average time complexity of add, put and remove is O(1).

Here, we use both arrays and linkedlist to solve the problem. First the index of the key is found. Then we use traverse through the linked
list if any to perform the operations.

Yes, the solution was run of leetcode.
*/
class MyHashSet {
Node arr[];
int size;
/** Initialize your data structure here. */
public MyHashSet() {
arr = new Node[100000];
size = 100000;
}

public void add(int key) {
int index = key%size;
Node root = arr[index];
if(root==null){
arr[index] = new Node(key);
}
else{
Node prev = null;
while(root!=null){
if(root.key == key){return;}
prev = root;
root = root.next;
}
prev.next = new Node(key);
}
}

public void remove(int key) {
int index = key%size;
Node root = arr[index];
if(root==null){
return;
}
else{
Node prev = null;
while(root!=null){
if(root.key==key){
if(prev==null){
arr[index] = root.next;
return;
}
else{
prev.next = root.next;
return;
}
}
prev = root;root = root.next;
}
}
}

/** Returns true if this set contains the specified element */
public boolean contains(int key) {
int index = key%size;
Node root = arr[index];
if(root==null){
return false;
}
else{
while(root!=null){
if(root.key==key){
return true;
}
root = root.next;
}

return false;
}
}
}

class Node{
int key;
Node next;

Node(int key){
this.key = key;
}
}

/**
* Your MyHashSet object will be instantiated and called as such:
* MyHashSet obj = new MyHashSet();
* obj.add(key);
* obj.remove(key);
* boolean param_3 = obj.contains(key);
*/
54 changes: 54 additions & 0 deletions HashSetOptimized.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
The time complexity of add, put and remove is O(1).

Here, we use 2d matrix with double hashing. The advantage here is while initiating the hasharray we only initiate the first level.
When any new element need to be added, then we initiate the 2nd dimension of that particular element.

Yes, the solution was run of leetcode.
*/
class MyHashSet {

boolean[][] hasharray;
int firsthash = 1000;
int secondhash = 1000;
/** Initialize your data structure here. */
public MyHashSet() {
hasharray = new boolean[firsthash][];
}

public void add(int key) {
int first = key%firsthash;
int second = key/secondhash;
if(hasharray[first]==null){
hasharray[first] = new boolean[secondhash];
}
hasharray[first][second] = true;
}

public void remove(int key) {
int first = key%firsthash;
int second = key/secondhash;
if(hasharray[first]!=null){
hasharray[first][second] = false;
}
}

/** Returns true if this set contains the specified element */
public boolean contains(int key) {
int first = key%firsthash;
int second = key/secondhash;
if(hasharray[first]!=null){
return hasharray[first][second];
}

return false;
}
}

/**
* Your MyHashSet object will be instantiated and called as such:
* MyHashSet obj = new MyHashSet();
* obj.add(key);
* obj.remove(key);
* boolean param_3 = obj.contains(key);
*/
57 changes: 57 additions & 0 deletions QueueUsingStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
The amortized time complexity is O(1) and the space complexity is O(N) where N is the number of elements stored.

Here, the idea is to use two stacks to implement queue. One for insertion and the other for pop of peek. When we want to peek or pop and
the second stack is empty we pop each element from first stack and insert into the second stack.

Yes, the solution passed all the test cases in leetcode
*/
class MyQueue {

Stack<Integer> stack;
Stack<Integer> queue;
/** Initialize your data structure here. */
public MyQueue() {
stack = new Stack<>();
queue = new Stack<>();
}

/** Push element x to the back of queue. */
public void push(int x) {
stack.push(x);
}

/** Removes the element from in front of queue and returns that element. */
public int pop() {
if(queue.isEmpty()){
while(stack.size()>0){
queue.push(stack.pop());
}
}
return queue.pop();
}

/** Get the front element. */
public int peek() {
if(queue.isEmpty()){
while(stack.size()>0){
queue.push(stack.pop());
}
}
return queue.peek();
}

/** Returns whether the queue is empty. */
public boolean empty() {
return queue.isEmpty()&&stack.isEmpty();
}
}

/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = new MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* boolean param_4 = obj.empty();
*/