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
73 changes: 73 additions & 0 deletions Problem1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Time Complexity : O(n) ammortized time complexity
// 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 maintian two stacks - an in-stack which is solely used for pushing elements into the queue and an out-stack which is solely used for
removing elements from the queue. Every insert operation into the queue is performed by pushing the element to the in-stack.
Before performing any pop/peek operation, we check if out-stack is empty, if so then we transfer all the elements from in stack to out stack.
Every peek and pop operation is performed on the out stack.
*/

public class MyQueue
{
Stack<int> inStack;
Stack<int> outStack;

public MyQueue()
{
inStack = new();
outStack = new();
}

public void Push(int x)
{
inStack.Push(x);
}

public int Pop()
{
if (outStack.Count == 0)
{
Transfer();
}

return outStack.Pop();
}

public int Peek()
{
if (outStack.Count == 0)
{
Transfer();
}

return outStack.Peek();
}

public bool Empty()
{
return inStack.Count == 0 && outStack.Count == 0;
}

private void Transfer()
{
while (inStack.Count != 0)
{
outStack.Push(inStack.Pop());
}
}
}

/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = new MyQueue();
* obj.Push(x);
* int param_2 = obj.Pop();
* int param_3 = obj.Peek();
* bool param_4 = obj.Empty();
*/
95 changes: 95 additions & 0 deletions Problem2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// 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

/*
I have implemented a hashmap using double hashing. By default, each bucket is allocated the value -1. If a key is not present, I return
-1 to the user. If key exists in the hashmap, the corresponding value is returned to the user if it's present, or -1 is returned.
*/

public class MyHashMap
{
private int[][] buckets;
private int bucketCount;
private int bucketSize;

public MyHashMap()
{
bucketCount = 1000;
bucketSize = 1000;
buckets = new int[bucketCount][];
}

public void Put(int key, int value)
{
int hash1 = HashFunction1(key);

if (buckets[hash1] == null)
{
if (hash1 == 0)
{
buckets[hash1] = new int[bucketSize + 1];
}

else
{
buckets[hash1] = new int[bucketSize];
}

Array.Fill(buckets[hash1], -1);
}

int hash2 = HashFunction2(key);

buckets[hash1][hash2] = value;

}

public int Get(int key)
{
int hash1 = HashFunction1(key);

if (buckets[hash1] == null)
{
return -1;
}

int hash2 = HashFunction2(key);

return buckets[hash1][hash2];
}

public void Remove(int key)
{
int hash1 = HashFunction1(key);

if (buckets[hash1] != null)
{
int hash2 = HashFunction2(key);

buckets[hash1][hash2] = -1;
}
}

private int HashFunction1(int key)
{
return key % bucketCount;
}

private int HashFunction2(int key)
{
return key / bucketSize;
}
}

/**
* Your MyHashMap object will be instantiated and called as such:
* MyHashMap obj = new MyHashMap();
* obj.Put(key,value);
* int param_2 = obj.Get(key);
* obj.Remove(key);
*/