From 2cc633e32f3848cbf056683c14d7472469c1cf0d Mon Sep 17 00:00:00 2001 From: tmujje Date: Thu, 22 Apr 2021 00:15:23 -0400 Subject: [PATCH 1/3] Added Design-1 assignment solutions --- MinStack.cs | 73 +++++++++++++++++++++++++++++++++++++ MyHashMap.cs | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 173 insertions(+) create mode 100644 MinStack.cs create mode 100644 MyHashMap.cs diff --git a/MinStack.cs b/MinStack.cs new file mode 100644 index 00000000..4e922127 --- /dev/null +++ b/MinStack.cs @@ -0,0 +1,73 @@ +using System; +using System.Collections.Generic; + +namespace Algorithms +{ + // Did this code successfully run on Leetcode : Yes + // Any problem you faced while coding this : Initially before today's class, + // I solved this using Dictionary(similar to HashMap in Java). Though I could get + // the Time complexity of Push and Pop functions to be O(1) + // my time complexity went to O(N) for Top() and Get() since I am + // trying to find out the Max of all the values in the HashMap everytime before GetMin() or Pop() + // This solution seemed more efficient, but that solution with HashMap worked on Leetcode too + // but with less effient space and time management than this one. + public class MinStack + { + /** initialize your data structure here. */ + private Stack originalStack; + private Stack minStack; + private int min; + + public MinStack() + { + originalStack = new Stack(); + minStack = new Stack(); + min = int.MaxValue; + minStack.Push(min); + } + + //Time Complexity - O(1) + //Space Complexity - O(N) + public void Push(int val) + { + //Every time a value is pushed, compare it with min value + //And store the corresponding min to minStack and original value to Original Stack + //So to keep the min element at the top of minStack. + min = Math.Min(min, val); + originalStack.Push(val); + minStack.Push(min); + } + + //Time Complexity - O(1) + //Space Complexity - O(N) + public void Pop() + { + originalStack.Pop(); + minStack.Pop(); + min = minStack.Peek(); // This step ensures that our min value getting updated after the original value and corresponding min are popped + } + + //Time Complexity - O(1) + //Space Complexity - O(1) + public int Top() + { + return originalStack.Peek(); + } + + //Time Complexity - O(1) + //Space Complexity - O(1) + 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(); + */ +} diff --git a/MyHashMap.cs b/MyHashMap.cs new file mode 100644 index 00000000..ae08f7fb --- /dev/null +++ b/MyHashMap.cs @@ -0,0 +1,100 @@ +namespace CustomDataStructures +{ + // Did this code successfully run on Leetcode : Yes + // Any problem you faced while coding this : One specific issue I faced in this problem is + // on how to differentiate between the actual value that needs to be stored being '0' and + // the initial default value of an int data type in the array. I explored two solutions for + // resolving this issue. The first one being initializing the hashMap with -1, but this solution takes O(N) + // and that defeats the purpose of HashMap. So I resorted to the second solution of storing the incoming actual value '0' as -2 + // with an arithmetic operation. And while getting the value in Get() method, if it is -2, + // I am returning '0' + + public class MyHashMap + { + + private int[][] arr; + private int buckets; + private int bucketItems; + + /** Initialize your data structure here. */ + public MyHashMap() + { + buckets = 1000; + bucketItems = 1000; + arr = new int[buckets][]; + } + + //Hash function for caluclating index of the buckets + public int bucket(int key) + { + return key % buckets; + } + + //Hash function for caluclating index of the bucket items (Nested index - index for the nested structure) + public int bucketItem(int key) + { + return key / bucketItems; + } + + //Time Complexity - O(1) + //Space Complexity - O(n) in this method - Since we have to just allocated BucketItems here + /** value will always be non-negative. */ + public void Put(int key, int value) + { + var firstIndex = bucket(key); + var secondIndex = bucketItem(key); + if (arr[firstIndex] == null) + { + if (firstIndex == 0) + { + // If the key is 10^6, then our hash function returns 1000 for the 0th index, + // where as we can only store 0 to 999 bucketItems, for this reason I am checking if the first index is 0, + // then allocate 1001 elements so that my index can go upto 1000 for the bucket items + arr[firstIndex] = new int[bucketItems + 1]; + } + else + { + arr[firstIndex] = new int[bucketItems]; + } + } + if (value == 0) + { + arr[firstIndex][secondIndex] = value - 2; + } + else + { + arr[firstIndex][secondIndex] = value; + } + + } + + //Time Complexity - O(1) + //Space Complexity - O(1) + /** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */ + public int Get(int key) + { + var firstIndex = bucket(key); + var secondIndex = bucketItem(key); + if (arr[firstIndex] == null || arr[firstIndex][secondIndex] == 0) + { + return -1; + } + if (arr[firstIndex][secondIndex] == -2) + { + return 0; + } + return arr[firstIndex][secondIndex]; + } + + //Time Complexity - O(1) + //Space Complexity - O(1) + /** Removes the mapping of the specified value key if this map contains a mapping for the key */ + public void Remove(int key) + { + var firstIndex = bucket(key); + var secondIndex = bucketItem(key); + if (arr[firstIndex] == null || arr[firstIndex][secondIndex] == -1) return; + arr[firstIndex][secondIndex] = -1; + } + } + } From 2474af650a1b4b9cc1dbebae88ef73bf1f3acd36 Mon Sep 17 00:00:00 2001 From: TejBharath Mujje Date: Thu, 30 Jul 2026 00:08:10 -0400 Subject: [PATCH 2/3] Implement MinStack Java --- MinStack.java | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 MinStack.java diff --git a/MinStack.java b/MinStack.java new file mode 100644 index 00000000..2ce6b2b9 --- /dev/null +++ b/MinStack.java @@ -0,0 +1,66 @@ +// Approach: +// The idea is to store current minumum in a min variable, keep track of previous mininums in a seperate stack for every push that happens on the original stack. +// So whenever a pop happens, we check if the popped element is minumum, if so we set the current minumum with the top of minStack, and pop this top element from minStack +// To prevent managing multiple same minimums, we follow this approach otherwise, we can achieve the same without the min variable, but through the minStack itself. + + +// Time Complexity: + +// push(), pop() - O(1) +// top, getMin() - O(1) + +// Space complexity: +// push(), pop() - O(1) +// top, getMin() - O(1) + +// Code +import java.util.Stack; + +class MinStack { + + Stack st; + Stack minSt; + int min; + + public MinStack() { + this.min = Integer.MAX_VALUE; + this.st = new Stack<>(); + this.minSt = new Stack<>(); + } + + + public void push(int value) { + if(min >= value) + { + minSt.push(min); + min = value; + } + + st.push(value); + } + + public void pop() { + if(min == st.pop()) + { + min = minSt.peek(); + minSt.pop(); + } + } + + 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(value); + * obj.pop(); + * int param_3 = obj.top(); + * int param_4 = obj.getMin(); + */ \ No newline at end of file From 8c6001e5ad7e21ae3208bf909eb35c6e80f90e24 Mon Sep 17 00:00:00 2001 From: pubali25 Date: Thu, 30 Jul 2026 21:28:15 -0400 Subject: [PATCH 3/3] Remove old files and Add HashSet java implementation --- MinStack.cs | 73 ------------------------------------ MyHashMap.cs | 100 ------------------------------------------------- MyHashSet.java | 55 +++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 173 deletions(-) delete mode 100644 MinStack.cs delete mode 100644 MyHashMap.cs create mode 100644 MyHashSet.java diff --git a/MinStack.cs b/MinStack.cs deleted file mode 100644 index 4e922127..00000000 --- a/MinStack.cs +++ /dev/null @@ -1,73 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace Algorithms -{ - // Did this code successfully run on Leetcode : Yes - // Any problem you faced while coding this : Initially before today's class, - // I solved this using Dictionary(similar to HashMap in Java). Though I could get - // the Time complexity of Push and Pop functions to be O(1) - // my time complexity went to O(N) for Top() and Get() since I am - // trying to find out the Max of all the values in the HashMap everytime before GetMin() or Pop() - // This solution seemed more efficient, but that solution with HashMap worked on Leetcode too - // but with less effient space and time management than this one. - public class MinStack - { - /** initialize your data structure here. */ - private Stack originalStack; - private Stack minStack; - private int min; - - public MinStack() - { - originalStack = new Stack(); - minStack = new Stack(); - min = int.MaxValue; - minStack.Push(min); - } - - //Time Complexity - O(1) - //Space Complexity - O(N) - public void Push(int val) - { - //Every time a value is pushed, compare it with min value - //And store the corresponding min to minStack and original value to Original Stack - //So to keep the min element at the top of minStack. - min = Math.Min(min, val); - originalStack.Push(val); - minStack.Push(min); - } - - //Time Complexity - O(1) - //Space Complexity - O(N) - public void Pop() - { - originalStack.Pop(); - minStack.Pop(); - min = minStack.Peek(); // This step ensures that our min value getting updated after the original value and corresponding min are popped - } - - //Time Complexity - O(1) - //Space Complexity - O(1) - public int Top() - { - return originalStack.Peek(); - } - - //Time Complexity - O(1) - //Space Complexity - O(1) - 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(); - */ -} diff --git a/MyHashMap.cs b/MyHashMap.cs deleted file mode 100644 index ae08f7fb..00000000 --- a/MyHashMap.cs +++ /dev/null @@ -1,100 +0,0 @@ -namespace CustomDataStructures -{ - // Did this code successfully run on Leetcode : Yes - // Any problem you faced while coding this : One specific issue I faced in this problem is - // on how to differentiate between the actual value that needs to be stored being '0' and - // the initial default value of an int data type in the array. I explored two solutions for - // resolving this issue. The first one being initializing the hashMap with -1, but this solution takes O(N) - // and that defeats the purpose of HashMap. So I resorted to the second solution of storing the incoming actual value '0' as -2 - // with an arithmetic operation. And while getting the value in Get() method, if it is -2, - // I am returning '0' - - public class MyHashMap - { - - private int[][] arr; - private int buckets; - private int bucketItems; - - /** Initialize your data structure here. */ - public MyHashMap() - { - buckets = 1000; - bucketItems = 1000; - arr = new int[buckets][]; - } - - //Hash function for caluclating index of the buckets - public int bucket(int key) - { - return key % buckets; - } - - //Hash function for caluclating index of the bucket items (Nested index - index for the nested structure) - public int bucketItem(int key) - { - return key / bucketItems; - } - - //Time Complexity - O(1) - //Space Complexity - O(n) in this method - Since we have to just allocated BucketItems here - /** value will always be non-negative. */ - public void Put(int key, int value) - { - var firstIndex = bucket(key); - var secondIndex = bucketItem(key); - if (arr[firstIndex] == null) - { - if (firstIndex == 0) - { - // If the key is 10^6, then our hash function returns 1000 for the 0th index, - // where as we can only store 0 to 999 bucketItems, for this reason I am checking if the first index is 0, - // then allocate 1001 elements so that my index can go upto 1000 for the bucket items - arr[firstIndex] = new int[bucketItems + 1]; - } - else - { - arr[firstIndex] = new int[bucketItems]; - } - } - if (value == 0) - { - arr[firstIndex][secondIndex] = value - 2; - } - else - { - arr[firstIndex][secondIndex] = value; - } - - } - - //Time Complexity - O(1) - //Space Complexity - O(1) - /** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */ - public int Get(int key) - { - var firstIndex = bucket(key); - var secondIndex = bucketItem(key); - if (arr[firstIndex] == null || arr[firstIndex][secondIndex] == 0) - { - return -1; - } - if (arr[firstIndex][secondIndex] == -2) - { - return 0; - } - return arr[firstIndex][secondIndex]; - } - - //Time Complexity - O(1) - //Space Complexity - O(1) - /** Removes the mapping of the specified value key if this map contains a mapping for the key */ - public void Remove(int key) - { - var firstIndex = bucket(key); - var secondIndex = bucketItem(key); - if (arr[firstIndex] == null || arr[firstIndex][secondIndex] == -1) return; - arr[firstIndex][secondIndex] = -1; - } - } - } diff --git a/MyHashSet.java b/MyHashSet.java new file mode 100644 index 00000000..c8c01b1f --- /dev/null +++ b/MyHashSet.java @@ -0,0 +1,55 @@ +class MyHashSet { + boolean[][] storage; + int primaryBuckets; + int secondaryBuckets; + + public MyHashSet() { + this.primaryBuckets = 1000; + this.secondaryBuckets = 1000; + this.storage = new boolean[primaryBuckets][]; + } + + private int getPrimaryHash(int key) + { + return key % primaryBuckets; + } + + private int getSecondaryHash(int key) + { + return key / secondaryBuckets; + } + + public void add(int key) + { + int primaryHash = getPrimaryHash(key); + if(storage[primaryHash] == null){ + if(primaryHash == 0) { + storage[primaryHash] = new boolean[secondaryBuckets+1]; + } + else{ + storage[primaryHash] = new boolean[secondaryBuckets]; + } + } + int secondaryHash = getSecondaryHash(key); + storage[primaryHash][secondaryHash] = true; + } + + public void remove(int key) { + int primaryHash = getPrimaryHash(key); + + if (storage[primaryHash] == null){ + return; + } + int secondaryHash = getSecondaryHash(key); + storage[primaryHash][secondaryHash] = false; + } + + public boolean contains(int key) { + int primaryHash = getPrimaryHash(key); + if (storage[primaryHash] == null){ + return false; + } + int secondaryHash = getSecondaryHash(key); + return storage[primaryHash][secondaryHash]; + } +} \ No newline at end of file