diff --git a/Design-hashset.java b/Design-hashset.java new file mode 100644 index 00000000..7c133696 --- /dev/null +++ b/Design-hashset.java @@ -0,0 +1,50 @@ +class MyHashSet { + private boolean[][] storage; + private int buckets; //primary index of primary array + private int bucketItems; //secondary index of seconday array + public MyHashSet() { + this.buckets = 1000; + this.bucketItems = 1000; + this.storage = new boolean[buckets][]; + + } + private int hash1(int key){ + return key % this.buckets; + } + private int hash2(int key){ + return key / this.bucketItems; + } + public void add(int key) { + int bucket = hash1(key); + int bucketItem = hash2(key); + if(storage[bucket] == null){ + if(bucket == 0) + storage[bucket] = new boolean[bucketItems+1]; //there are 1M+1 items for bucket 0 + else + storage[bucket] = new boolean[bucketItems]; + } + storage[bucket][bucketItem] = true; + } + + public void remove(int key) { + int bucket = hash1(key); + int bucketItem = hash2(key); + if(storage[bucket] == null) return; + storage[bucket][bucketItem] = false; + } + + public boolean contains(int key) { + int bucket = hash1(key); + int bucketItem = hash2(key); + if(storage[bucket] == null) return false; + return storage[bucket][bucketItem]; + } +} + +/** + * Your MyHashSet object will be instantiated and called as such: + * MyHashSet obj = new MyHashSet(); + * obj.add(key); + * obj.remove(key); + * boolean param_3 = obj.contains(key); + */ \ No newline at end of file diff --git a/Min-stack.java b/Min-stack.java new file mode 100644 index 00000000..1b71cb3b --- /dev/null +++ b/Min-stack.java @@ -0,0 +1,40 @@ +class MinStack { + Stack st; + Stack minSt; + int min; + public MinStack() { + this.st = new Stack<>(); + this.minSt = new Stack<>(); + this.min = Integer.MAX_VALUE; + this.minSt.push(min); + } + + public void push(int val) { + min = Math.min(min, val); + st.push(val); + minSt.push(min); + } + + public void pop() { + st.pop(); + minSt.pop(); + min = minSt.peek(); //dont forget to reset min once pop happens + } + + public int top() { + return st.peek(); + } + + public int getMin() { + return min; + } +} + +/** + * Your MinStack object will be instantiated and called as such: + * MinStack obj = new MinStack(); + * obj.push(val); + * obj.pop(); + * int param_3 = obj.top(); + * int param_4 = obj.getMin(); + */ \ No newline at end of file diff --git a/README.md b/README.md index 6907f832..6e74bf62 100644 --- a/README.md +++ b/README.md @@ -3,11 +3,15 @@ Explain your approach in **three sentences only** at top of your code # Design-1 ## Problem 1:(https://leetcode.com/problems/design-hashset/) - - +Need to design Hashset w/o in-built libraries. Implement add(), contains(), remove(). +Use double hashing technique in a primary array of references, initialize secondary array of boolean values +Don't forget that that there is 1M+1 elements for which extra bucket element will be inserted ## Problem 2: Design MinStack (https://leetcode.com/problems/min-stack/) +Maintain 2 stacks - stack , minStack. +minStack will always have a MAX_VALUE. dont forget to reset min once pop happens bcz once element is popped it cannot be the min. +