diff --git a/MyHashMap.java b/MyHashMap.java new file mode 100644 index 00000000..6902fd27 --- /dev/null +++ b/MyHashMap.java @@ -0,0 +1,51 @@ +class MyHashMap { + int primaryBuckets; + int secondaryBuckets; + int[][] storage; + + public MyHashMap() { + this.primaryBuckets = 1000; + this.secondaryBuckets = 1000; + this.storage = new int[primaryBuckets][]; + } + + private int getPrimaryHash(int key){ + return key % primaryBuckets; + } + + private int getSecondaryHash(int key){ + return key / secondaryBuckets; + } + + public void put(int key, int value) { + int primaryIndex = getPrimaryHash(key); + if(storage[primaryIndex] == null){ + if(primaryIndex == 0){ + storage[primaryIndex] = new int[secondaryBuckets+1]; + }else{ + storage[primaryIndex] = new int[secondaryBuckets]; + } + Arrays.fill(storage[primaryIndex], -1); + } + int secondaryIndex = getSecondaryHash(key); + storage[primaryIndex][secondaryIndex] = value; + } + + public void remove(int key) { + int primaryIndex = getPrimaryHash(key); + if(storage[primaryIndex] == null){ + return; + } + int secondaryIndex = getSecondaryHash(key); + storage[primaryIndex][secondaryIndex] = -1; + } + + public int get(int key) { + int primaryIndex = getPrimaryHash(key); + if(storage[primaryIndex] == null){ + return -1; + } + int secondaryIndex = getSecondaryHash(key); + return storage[primaryIndex][secondaryIndex]; + } +} diff --git a/MyQueue.java b/MyQueue.java new file mode 100644 index 00000000..4f1280c5 --- /dev/null +++ b/MyQueue.java @@ -0,0 +1,32 @@ +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(empty()) return -1; + peek(); + 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(); + } +}