diff --git a/MinStack.java b/MinStack.java new file mode 100644 index 00000000..de032a88 --- /dev/null +++ b/MinStack.java @@ -0,0 +1,34 @@ +import java.util.Stack; + +class MinStack { + + Stack storageStack; + Stack minStack; + int min; + + public MinStack() { + storageStack = new Stack<>(); + minStack = new Stack<>(); + min = Integer.MAX_VALUE; + } + + public void push(int val) { + storageStack.push(val); + min = Math.min(min, val); + minStack.push(min); + } + + public void pop() { + storageStack.pop(); + minStack.pop(); + min = minStack.isEmpty() ? Integer.MAX_VALUE : minStack.peek(); + } + + public int top() { + return storageStack.peek(); + } + + public int getMin() { + return minStack.peek(); + } +} \ No newline at end of file diff --git a/MyHashSet.java b/MyHashSet.java new file mode 100644 index 00000000..fc586c56 --- /dev/null +++ b/MyHashSet.java @@ -0,0 +1,56 @@ +class MyHashSet { + + int primaryArraySize; + int secondaryArraySize; + + boolean[][] customHashArray; + + public MyHashSet() { + this.primaryArraySize = 1000; + this.secondaryArraySize = 1000; + this.customHashArray = new boolean[primaryArraySize][]; + } + + public int getPrimaryHashValue(int key) { + return key % primaryArraySize; + } + + public int getSecondaryHashValue(int key) { + return key / secondaryArraySize; + } + + public void add(int key) { + + int primaryPos = getPrimaryHashValue(key); + int secondaryPos = getSecondaryHashValue(key); + + if(customHashArray[primaryPos] == null) { + if(primaryPos == 0 && secondaryPos == 1000) { + customHashArray[primaryPos] = new boolean[secondaryArraySize + 1]; + } else { + customHashArray[primaryPos] = new boolean[secondaryArraySize]; + } + } + customHashArray[primaryPos][secondaryPos] = true; + } + + public void remove(int key) { + int primaryPos = getPrimaryHashValue(key); + int secondaryPos = getSecondaryHashValue(key); + if(customHashArray[primaryPos] != null) { + customHashArray[primaryPos][secondaryPos] = false; + } + + } + + public boolean contains(int key) { + int primaryPos = getPrimaryHashValue(key); + + if(customHashArray[primaryPos] == null) { + return false; + } + int secondaryPos = getSecondaryHashValue(key); + return customHashArray[primaryPos][secondaryPos]; + + } +}