From 897d813fd68d8f01169c1aa510a209ae7e4ffba4 Mon Sep 17 00:00:00 2001 From: Sreeja-99 <75175169+Sreeja-99@users.noreply.github.com> Date: Sat, 27 Dec 2025 23:09:42 -0600 Subject: [PATCH 1/2] HashSet_Design Implemented a MyHashSet class using a two-dimensional array to store elements. The class includes methods to add, remove, and check for the existence of elements with O(1) time and space complexity. --- HashSet.java | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 HashSet.java diff --git a/HashSet.java b/HashSet.java new file mode 100644 index 00000000..d912d25f --- /dev/null +++ b/HashSet.java @@ -0,0 +1,77 @@ +//s30: https://www.thes30.com/problem/5c91d568fdc132001682c131/solutions +//https://www.youtube.com/watch?v=G1biLTsvrWE +// Used Two dimensional array with 1000 rows and 1000 cols. In total,we can store 10^6 elements as given in input. +//Identifying row using Hash1 method. +//Identifying col using Hash2 method. +//Add- place an element at that place. //Remove- removing an element from that place. //Check- checking an element from the respective place. +//TC: O(1) +//SC: O(1) +class MyHashSet { + int primaryBuckets; + int secondaryBuckets; + boolean[][] storage; + + public MyHashSet() { + this.primaryBuckets=1000; + this.secondaryBuckets=1000; + this.storage=new boolean[1000][]; + + } + private int hash1(int key){ + return key%1000; + } + + private int hash2(int key){ + return key/1000; + } + + public void add(int key) { + int h1=hash1(key); + //To handle edge case.. 0 to 1000000 contains 1000001 elements. So, add extra size for one array to hold that extra element + if(storage[h1]==null){ + if(h1==0){ + storage[h1]=new boolean[secondaryBuckets+1]; + }else{ + storage[h1]=new boolean[secondaryBuckets]; + } + + } + + int h2=hash2(key); + storage[h1][h2]=true; + } + + //TC: O(1) + //SC: O(1) + public void remove(int key) { + int h1=hash1(key); + if(storage[h1]!=null){ + int h2=hash2(key); + storage[h1][h2]=false; + } + + } + //TC: O(1) + //SC: O(1) + public boolean contains(int key) { + int h1=hash1(key); + if(storage[h1]!=null){ + int h2=hash2(key); + if(storage[h1][h2]==false){ + return false; + }else{ + return true; + } + }else{ + return false; + } + } +} + +/** + * 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); + */ From af8a8309f487655c4c5e3c1c75de712311b6ca0e Mon Sep 17 00:00:00 2001 From: Sreeja-99 <75175169+Sreeja-99@users.noreply.github.com> Date: Sat, 27 Dec 2025 23:15:23 -0600 Subject: [PATCH 2/2] MinStack Implement MinStack with push, pop, top, and getMin methods. --- MinStack.java | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 MinStack.java diff --git a/MinStack.java b/MinStack.java new file mode 100644 index 00000000..f79d4bce --- /dev/null +++ b/MinStack.java @@ -0,0 +1,53 @@ +//Using two stacks: MainStack and MinStack +//Maintaing min variable. +//For every addition, pushing that value to mainStack. Caluclating minimum. And pushing that min to minStack which means min element for all the elements which present in Stack. +//While removing, removing from both the stacks and peeking the top element to min variable. +//getMin: returing min variable +//top: Peeking top element of mainStack. + +//TC: O(1); SC:O(1) + +class MinStack { + Stack stack; + Stack minStack; + int min=Integer.MAX_VALUE; + + + public MinStack() { + this.stack=new Stack<>(); + this.minStack=new Stack<>(); + minStack.push(min); + + } + + public void push(int val) { + min=Math.min(min,val); + stack.push(val); + minStack.push(min); + + } + + public void pop() { + stack.pop(); + minStack.pop(); + min=minStack.peek(); + + } + + public int top() { + return stack.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(); + */