diff --git a/HashMap.java b/HashMap.java new file mode 100644 index 00000000..ee14a6d3 --- /dev/null +++ b/HashMap.java @@ -0,0 +1,85 @@ +//Approach +//TC: O(1) +//SC: O(n) + + +class MyHashMap { + Node[] storage; + int buckets; + + class Node { + int key; + int value; + Node next; + + public Node(int key, int value) { + this.key = key; + this.value = value; + } + } + + public MyHashMap() { + this.buckets = 1000; + this.storage = new Node[buckets]; + } + + public int getHash(int key) { + return key % buckets; + } + + public 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 primary = getHash(key); + if (storage[primary] == null) { + storage[primary] = new Node(-1, -1); + storage[primary].next = new Node(key, value); + return; + } + Node prev = getPrev(storage[primary], key); + if (prev.next == null) { + prev.next = new Node(key, value); + } else { + prev.next.value = value; + } + } + + public int get(int key) { + int hash = getHash(key); + if (storage[hash] == null) + return -1; + Node prev = getPrev(storage[hash], key); + if (prev.next == null) { + return -1; + } + return prev.next.value; + } + + public void remove(int key) { + int hash = getHash(key); + if (storage[hash] == null) { + return; + } + Node prev = getPrev(storage[hash], key); + if (prev.next == null) { + return; + } + prev.next = prev.next.next; + } +} + +/** + * Your MyHashMap object will be instantiated and called as such: + * MyHashMap obj = new MyHashMap(); + * obj.put(key,value); + * int param_2 = obj.get(key); + * obj.remove(key); + */ \ No newline at end of file diff --git a/QueueImpleWithStack.java b/QueueImpleWithStack.java new file mode 100644 index 00000000..c6e11aa2 --- /dev/null +++ b/QueueImpleWithStack.java @@ -0,0 +1,50 @@ +import java.util.*; + +// Approach: +// TC:O(1), worstcase - O(n) +// SC:O(n) +class MyQueue { + + Stack inSt; + Stack outSt; + + public MyQueue() { + this.inSt = new Stack<>(); + this.outSt = new Stack<>(); + } + + public void push(int x) { + inSt.push(x); + } + + public int pop() { + if (outSt.isEmpty()) { + while (!inSt.isEmpty()) { + outSt.push(inSt.pop()); + } + } + return outSt.pop(); + } + + public int peek() { + if (outSt.isEmpty()) { + while (!inSt.isEmpty()) { + outSt.push(inSt.pop()); + } + } + return outSt.peek(); + } + + public boolean empty() { + return inSt.isEmpty() && outSt.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(); + */ \ No newline at end of file