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

Completed Design-2 [2024] Problem4 #2029

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
51 changes: 51 additions & 0 deletions 2024_Problem4_Impl_Queue_using_Stack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//232. Implement Queue using Stacks - https://leetcode.com/problems/implement-queue-using-stacks/description/
//Time Complexity: All operation takes constant time O(1)
//Space Complexity: O(n)

class MyQueue {

Stack<Integer> stack;
Stack<Integer> reverseStack;

public MyQueue() {
this.stack = new Stack<>();
this.reverseStack = new Stack<>();
}

public void push(int x) {
stack.push(x); // Push element x to the end
}

private void reverseStack(){
while(!stack.empty()){
reverseStack.push(stack.pop()); //pop elements from main stack and push in reverseStack in reverse order
}
}

/** Removes the element from in front of queue and returns that element. */
public int pop() {
int popElement = -1; //default value of stack if empty

if(reverseStack.empty())
reverseStack(); //populate the reverse stack

popElement = reverseStack.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(reverseStack.empty())
reverseStack(); //populate the reverse stack

peekElement = reverseStack.peek(); //peeking the first element in queue w/o poping
return peekElement;
}

/** Returns whether the queue is empty. */
public boolean empty() {
return (stack.empty() && reverseStack.empty());
}
}
76 changes: 76 additions & 0 deletions 2024_Problem5_Design_HashMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
class MyHashMap {

//Here primary DS is array so we need to store the Node to get the secondary DS
class Node{
int key;
int value;
Node next;

public Node(int key, int value){
this.key = key;
this.value = value;
}
}

Node[] storage;
int buckets;

public MyHashMap() {
//increased the primary data structure size to reduce the secondary DS
//this way way we are reducing no. of collisions
this.buckets = 10000;
this.storage = new Node[buckets];
}
private int getPrimaryHash(int key){
return key % buckets;
}

private Node getPrev(Node head, int key){
Node prev = null;
Node curr = head;
while(curr != null && curr.key != key){
prev = curr;
curr = curr.next;
}
return prev;
}

public void put(int key, int value) {
int primaryIndex = getPrimaryHash(key);
if(storage[primaryIndex] == null){
storage[primaryIndex] = new Node(-1,-1); //creating dummy node to get prev node for the 1st Node
}
Node prev = getPrev(storage[primaryIndex],key);
if(prev.next == null){ //last node and couldn't find the node
prev.next = new Node(key,value);
} else {
prev.next.value = value;
}
}

public int get(int key) {
int primaryIndex = getPrimaryHash(key);
if(storage[primaryIndex] == null){
return -1;
}
Node prev = getPrev(storage[primaryIndex],key);
if(prev.next == null){
return -1;
}
return prev.next.value;
}

public void remove(int key) {
int primaryIndex = getPrimaryHash(key);
if(storage[primaryIndex] == null){
return;
}
Node prev = getPrev(storage[primaryIndex],key);
if(prev.next == null){
return;
}
Node curr = prev.next;
prev.next=curr.next;
curr.next = null;
}
}