diff --git a/Problem1.cs b/Problem1.cs new file mode 100644 index 00000000..f05271d8 --- /dev/null +++ b/Problem1.cs @@ -0,0 +1,86 @@ +// Time Complexity : O(1) +// Space Complexity : O(n) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + + +// Your code here along with comments explaining your approach + +// Since the total number of keys can be up to 10^6, I am using an array of arrays of boolean type with the primary array being of size 1000 +// and the secondary array being of size 1000. I am following the double hashing based approach so that we can perform Add, Contains and Remove +// in O(1) time complexity. + +public class MyHashSet +{ + + private bool[][] buckets; + private int bucketSize; + private int bucketCount; + + public MyHashSet() + { + bucketSize = 1000; + bucketCount = 1000; + buckets = new bool[bucketCount][]; + } + + private int HashFunction1(int key) + { + return key % bucketCount; + } + + private int HashFunction2(int key) + { + return key / bucketSize; + } + + public void Add(int key) + { + int hash1 = HashFunction1(key); + + if (buckets[hash1] == null) + { + if (hash1 == 0) + { + buckets[hash1] = new bool[bucketSize + 1]; + } + + else + { + buckets[hash1] = new bool[bucketSize]; + } + } + + int hash2 = HashFunction2(key); + + buckets[hash1][hash2] = true; + } + + public void Remove(int key) + { + int hash1 = HashFunction1(key); + + if (buckets[hash1] != null) + { + int hash2 = HashFunction2(key); + + buckets[hash1][hash2] = false; + } + } + + public bool Contains(int key) + { + int hash1 = HashFunction1(key); + int hash2 = HashFunction2(key); + + return buckets[hash1] != null && buckets[hash1][hash2]; + } +} + +/** + * Your MyHashSet object will be instantiated and called as such: + * MyHashSet obj = new MyHashSet(); + * obj.Add(key); + * obj.Remove(key); + * bool param_3 = obj.Contains(key); + */ \ No newline at end of file diff --git a/Problem2.cs b/Problem2.cs new file mode 100644 index 00000000..64078945 --- /dev/null +++ b/Problem2.cs @@ -0,0 +1,70 @@ +// Time Complexity : O(n) +// Space Complexity : O(n) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + + +// Your code here along with comments explaining your approach +/* +I have initialized a stack of integer arrays and store each element along with the corresponding minimum element in the stack so far +for that particular element. Before I push each value and minimum pair, I check if the current value is less than the minimum element so +far. If yes, I update the minimum element with the value. Similarly while popping, I check if the popped value is the minimum element, if yes +I update the minimum value with the minimum value corresponding with the top element or Int32.MaxValue if the stack is empty. +*/ + +public class MinStack +{ + Stack minStack; + int minimumSoFar; + + public MinStack() + { + minStack = new(); + minimumSoFar = Int32.MaxValue; + } + + public void Push(int val) + { + if (val < minimumSoFar) + { + minimumSoFar = val; + } + + minStack.Push(new int[] { val, minimumSoFar }); + } + + public void Pop() + { + if (minimumSoFar == minStack.Pop()[0]) + { + if (minStack.Count != 0) + { + minimumSoFar = GetMin(); + } + + else + { + minimumSoFar = Int32.MaxValue; + } + } + } + + public int Top() + { + return minStack.Peek()[0]; + } + + public int GetMin() + { + return minStack.Peek()[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(); + */ \ No newline at end of file