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 #2036

Open
wants to merge 1 commit 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
107 changes: 107 additions & 0 deletions MyHashMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Time Complexity :put: O(1)
// get: O(1)
// remove: O(1)
// Space Complexity :put: O(1)
// get: O(1)
// remove: O(1)
// constructor: O(n) (because we are creating storage array)
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this : nothing



class MyHashMap {

class Node {
int key;
int value;
Node next;

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

}

Node[] storage;
int buckets;

public MyHashMap() {
this.buckets=10000;
this.storage = new Node[buckets];
}

private int getPrimaryHash(int key){
return key % buckets;
}

private Node getPrevNode(Node head, int key){
Node prev = null;
Node curr = head;

while(curr != null && curr.key != key){
prev = curr;
curr = prev.next;
}
return prev;
}

public void put(int key, int value) {
int pIndex = getPrimaryHash(key);
if(storage[pIndex] == null){
storage[pIndex] = new Node(-1,-1);
}
Node prev= getPrevNode(storage[pIndex], key);
if(prev.next == null){
prev.next = new Node(key , value);
} else {
prev.next.value = value;
}

}

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

public void remove(int key) {
int pIndex = getPrimaryHash(key);
if(storage[pIndex]== null){
return;
}
Node prev = getPrevNode(storage[pIndex], key);
if(prev.next == null){
return;
} else {
Node temp = prev.next;
prev.next = temp.next;
temp = null;
}
}


public static void main(String args[])
{
MyHashMap obj = new MyHashMap();
obj.put(2,10);
obj.put(4,10);
obj.put(2,12);
obj.put(5,30);
obj.put(6,3000);
obj.put(7,4000);
obj.remove(4);
System.out.println("Value corresponding to key 6 is "+obj.get(6));

}
}

63 changes: 63 additions & 0 deletions MyQueue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Time Complexity : empty: O(1)
// push: O(1)
// pop: O(1)
// peek: O(1)
// Space Complexity : empty: O(1)
// push: O(1)
// pop: O(1)
// peek: O(1)
// constructor: O(2n) because we're creating stacks
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this : nothing

import java.util.Stack;

class MyQueue {
Stack<Integer> inStk;
Stack<Integer> outStk;
public MyQueue() {
this.inStk= new Stack<>();
this.outStk= new Stack<>();
}

public void push(int x) {
inStk.push(x);
}

public int pop() {
if(empty()) return -1;
if(outStk.isEmpty()){
while(!inStk.isEmpty()){
outStk.push(inStk.pop());
}
}
return outStk.pop();
}

public int peek() {
if(outStk.isEmpty()){
while(!inStk.isEmpty()){
outStk.push(inStk.pop());
}
}
return outStk.peek();
}

public boolean empty() {
return inStk.isEmpty() && outStk.isEmpty();
}


public static void main(String args[])
{
MyQueue queue = new MyQueue();
queue.push(10);
queue.push(20);
queue.push(30);
queue.push(9);
queue.push(7);
queue.push(6);
System.out.println(queue.pop() + " Popped from stack");
System.out.println(queue.peek() + " is top element of the stack");
}
}