diff --git a/ImplementHashMap.java b/ImplementHashMap.java new file mode 100644 index 00000000..3fc1da5d --- /dev/null +++ b/ImplementHashMap.java @@ -0,0 +1,81 @@ +//Approach: Similar to HashSet implementation try using double-hashing method +// to handle collisions + +// Time Complexity: O(1) +// Space Complexity: O(1) + +class MyHashMap { + + int[][] arr; + int buckets; + int bucketItems; + + public MyHashMap() { + buckets = 1000; + bucketItems = 1000; + arr = new int[buckets][]; + } + + public int getPrimaryHash(int key) + { + return key % buckets; + } + + public int getSecondaryHash(int key) + { + return key / bucketItems; + } + + public void put(int key, int value) { + var firstIndex = getPrimaryHash(key); + var secondIndex = getSecondaryHash(key); + if (arr[firstIndex] == null) + { + if (firstIndex == 0) + { + arr[firstIndex] = new int[bucketItems + 1]; + } + else + { + arr[firstIndex] = new int[bucketItems]; + } + } + if (value == 0) + { + arr[firstIndex][secondIndex] = value - 2; + } + else + { + arr[firstIndex][secondIndex] = value; + } + } + + public int get(int key) { + var firstIndex = getPrimaryHash(key); + var secondIndex = getSecondaryHash(key); + + if (arr[firstIndex] == null || arr[firstIndex][secondIndex] == 0) + { + return -1; + } + if (arr[firstIndex][secondIndex] == -2) + { + return 0; + } + return arr[firstIndex][secondIndex]; + } + + public void remove(int key) { + var firstIndex = getPrimaryHash(key); + var secondIndex = getSecondaryHash(key); + if (arr[firstIndex] == null || arr[firstIndex][secondIndex] == -1) return; + arr[firstIndex][secondIndex] = -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 diff --git a/ImplementQueueUsingStacks.java b/ImplementQueueUsingStacks.java new file mode 100644 index 00000000..b87e74cd --- /dev/null +++ b/ImplementQueueUsingStacks.java @@ -0,0 +1,50 @@ +//Approach: The idea is to hold elements in one stack, until a pop() or peek() happens and copy the elements from first stack to another and then pop() or peek() + +// Time Complexity for Pop() and Peek() is O(n) (as linear traversal is involved) +// Time Complexity for Push() and empty() is O(1) + +// Space Complexity: O(n), as we have to maintain another stack to temporarily hold elements for pop() and peek() operations + +class MyQueue { + + Stack firstStack; + Stack secondStack; + + public MyQueue() { + firstStack = new Stack<>(); + secondStack = new Stack<>(); + } + + public void push(int x) { + firstStack.push(x); + } + + public int pop() { + peek(); + return secondStack.pop(); + } + + public int peek() { + if(secondStack.size() == 0){ + while(firstStack.size() != 0) + { + secondStack.push(firstStack.pop()); + } + } + + return secondStack.peek(); + } + + public boolean empty() { + return firstStack.size() == 0 && secondStack.size() == 0; + } +} + +/** + * 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