diff --git a/232. Implement Queue using Stacks/MyQueue.java b/232. Implement Queue using Stacks/MyQueue.java new file mode 100644 index 00000000..184c72fa --- /dev/null +++ b/232. Implement Queue using Stacks/MyQueue.java @@ -0,0 +1,57 @@ +import java.util.*; + +class MyQueue { + + Stack inStack; + Stack outStack; + + public MyQueue() { + + this.inStack = new Stack<>(); + this.outStack = new Stack<>(); + + } + + public void push(int x) { + inStack.push(x); + } + + public int pop() { + + if (outStack.isEmpty() && inStack.isEmpty()) { + return -1; + } + + peek(); + return outStack.pop(); + + } + + public int peek() { + + if (outStack.isEmpty()) { + while (!inStack.isEmpty()) { + outStack.push(inStack.pop()); + } + } + return outStack.peek(); + } + + public boolean empty() { + + return inStack.isEmpty() && outStack.isEmpty(); + + } +} + +// TC = O(n) +// SC = O(n) + +/** + * 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 diff --git a/706. Design HashMap/MyHashMap.java b/706. Design HashMap/MyHashMap.java new file mode 100644 index 00000000..06dbd485 --- /dev/null +++ b/706. Design HashMap/MyHashMap.java @@ -0,0 +1,78 @@ +import java.util.*; + +class MyHashMap { + + int primaryBucket; + int secondaryBucket; + int[][] storage; + + public MyHashMap() { + + this.primaryBucket = 1000; + this.secondaryBucket = 1000; + this.storage = new int[primaryBucket +1][]; + + } + + public int getPrimaryHash(int key) { + + return key / primaryBucket; + + } + + public int getSecondaryHash(int key) { + + return key % secondaryBucket; + + } + + public void put(int key, int value) { + + int primaryIndex = getPrimaryHash(key); + + if (storage[primaryIndex] == null) { + if (primaryIndex == 0) { + storage[primaryIndex] = new int[secondaryBucket + 1]; + } else { + storage[primaryIndex] = new int[secondaryBucket]; + } + Arrays.fill(storage[primaryIndex], -1); + } + + int secondaryIndex = getSecondaryHash(key); + storage[primaryIndex][secondaryIndex] = value; + + } + + public int get(int key) { + + int primaryIndex = getPrimaryHash(key); + if (storage[primaryIndex] == null) + return -1; + + int secondaryIndex = getSecondaryHash(key); + return storage[primaryIndex][secondaryIndex]; + + } + + public void remove(int key) { + + int primaryIndex = getPrimaryHash(key); + if (storage[primaryIndex] == null) + return; + + int secondaryIndex = getSecondaryHash(key); + storage[primaryIndex][secondaryIndex] = -1; + } +} + +// TC - O(1) +// SC - O(10^6) //As it is demmand on allocation so actual is O(1) + +/** + * 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