From a2166546a9a8bfba08bfdd00f37687b373e9d4c4 Mon Sep 17 00:00:00 2001 From: sabyasachi bisoyi Date: Tue, 23 Dec 2025 10:24:17 -0800 Subject: [PATCH 1/2] Design-1 problem Sabyasachi Bisoyi --- Sample.java | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 67 insertions(+), 3 deletions(-) diff --git a/Sample.java b/Sample.java index 1739a9cb..dd8c77ba 100644 --- a/Sample.java +++ b/Sample.java @@ -1,7 +1,71 @@ -// Time Complexity : -// Space Complexity : -// Did this code successfully run on Leetcode : +// Time Complexity : O(n) -> as we have the base impl of LinkedList it's time complexity refers here +// Space Complexity : O(n) -> where n is the number of inerstions, range for hashing can be constant +// Did this code successfully run on Leetcode : Yes // Any problem you faced while coding this : +//I started with int[] arr = new int[Integer.MAX_VALUE] +//where time complexity is O(1) but got memory limit excceded +//So proceed with having limited number of keys for range, and each array can accomdate a list // Your code here along with comments explaining your approach +class MyHashSet { + Bucket[] bucket; + + public MyHashSet() { + this.bucket = new Bucket[1000]; + for(int i = 0;i<1000;i++) + { + this.bucket[i] = new Bucket(); + } + } + + protected int _hash(int key) + { + return (key % 1000); + } + + public void add(int key) { + int bucketIndex = this._hash(key); + this.bucket[bucketIndex].insert(key); + } + + public void remove(int key) { + int bucketIndex = this._hash(key); + this.bucket[bucketIndex].remove(key); + } + + public boolean contains(int key) { + int bucketIndex = this._hash(key); + return this.bucket[bucketIndex].exists(key); + } +} + +class Bucket +{ + private LinkedList list; + + public Bucket() + { + list = new LinkedList(); + } + + public void insert(Integer key) + { + int index = this.list.indexOf(key); + if(index==-1) + { + this.list.addFirst(key); + } + } + + public void remove(Integer key) + { + this.list.remove(key); + } + + public boolean exists(Integer key) + { + int index = this.list.indexOf(key); + return (index!=-1); + } +} From 48a55f2196155bf127b8eb6c3c89d50af92ad95f Mon Sep 17 00:00:00 2001 From: sabyasachi bisoyi Date: Tue, 23 Dec 2025 16:54:11 -0800 Subject: [PATCH 2/2] Min Stack Impl -> Sabyasachi Bisoyi --- Sample.java | 92 +++++++++++++++++++++++------------------------------ 1 file changed, 39 insertions(+), 53 deletions(-) diff --git a/Sample.java b/Sample.java index dd8c77ba..bf6ae167 100644 --- a/Sample.java +++ b/Sample.java @@ -1,71 +1,57 @@ -// Time Complexity : O(n) -> as we have the base impl of LinkedList it's time complexity refers here -// Space Complexity : O(n) -> where n is the number of inerstions, range for hashing can be constant +// Time Complexity : O(1) +// Space Complexity : O(n) // Did this code successfully run on Leetcode : Yes // Any problem you faced while coding this : - -//I started with int[] arr = new int[Integer.MAX_VALUE] -//where time complexity is O(1) but got memory limit excceded -//So proceed with having limited number of keys for range, and each array can accomdate a list +// In push fucntion calculating minVal, I did mistakely poped instared of peek +// It emptied the Stack when single element is present // Your code here along with comments explaining your approach -class MyHashSet { - Bucket[] bucket; +class MinStack { + //we will store two vals in int[] + //First one current value, second one min element till now + Stack stack; + public MinStack() { + stack = new Stack(); + } - public MyHashSet() { - this.bucket = new Bucket[1000]; - for(int i = 0;i<1000;i++) + public void push(int val) { + if(stack.isEmpty()) { - this.bucket[i] = new Bucket(); + stack.push(new int[]{val,val}); + return; } + //Calc min value till now + int minVal = stack.peek()[1]; + //Calc curr min by comparinging minVal and incoming val + int currMin = Math.min(minVal,val); + //push the {val,currMin} into stack + stack.push(new int[]{val,currMin}); } - protected int _hash(int key) - { - return (key % 1000); - } - - public void add(int key) { - int bucketIndex = this._hash(key); - this.bucket[bucketIndex].insert(key); - } - - public void remove(int key) { - int bucketIndex = this._hash(key); - this.bucket[bucketIndex].remove(key); - } + public void pop() { + //O(1) time + stack.pop(); - public boolean contains(int key) { - int bucketIndex = this._hash(key); - return this.bucket[bucketIndex].exists(key); } -} -class Bucket -{ - private LinkedList list; + public int top() { + //O(1) time + return stack.peek()[0]; - public Bucket() - { - list = new LinkedList(); } - public void insert(Integer key) - { - int index = this.list.indexOf(key); - if(index==-1) - { - this.list.addFirst(key); - } - } + public int getMin() { + //O(1) time + return stack.peek()[1]; - public void remove(Integer key) - { - this.list.remove(key); - } - - public boolean exists(Integer key) - { - int index = this.list.indexOf(key); - return (index!=-1); } } + +/** + * 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(); + */