Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions Problem1.cs
Original file line number Diff line number Diff line change
@@ -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);
*/
70 changes: 70 additions & 0 deletions Problem2.cs
Original file line number Diff line number Diff line change
@@ -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<int[]> 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();
*/