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
61 changes: 61 additions & 0 deletions MinStack.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Time Complexity : O(1)
// Space Complexity :O(n) - Extra stacks
// Did this code successfully run on Leetcode : Yes

// Any problem you faced while coding this :
//NA

// Your code here along with comments explaining your approach
// I have maintained two stack one is original and other one is Min stack
// I will add vals in stack and for min stack compare the values like if the new value is less than top element of the stack then only insert it in the min stack else insert last min value.

public class MinStack
{

private Stack<int> stack;
private Stack<int> minStack;
public MinStack()
{
stack = new Stack<int>();
minStack = new Stack<int>();
}

public void Push(int val)
{
stack.Push(val);

if (minStack.Count == 0 || val < minStack.Peek())
{
minStack.Push(val);
}
else
{
minStack.Push(minStack.Peek());
}
}

public void Pop()
{
stack.Pop();
minStack.Pop();
}

public int Top()
{
return stack.Peek();
}

public int GetMin()
{
return minStack.Peek();
}
}

/**
* 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();
*/
56 changes: 56 additions & 0 deletions Sample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Time Complexity : O(1)
// Space Complexity :O(n) - 1bit is allocated per key
// Did this code successfully run on Leetcode : Yes

// Any problem you faced while coding this :

//Yes I had to spend some time on understanding how the hashing works, I havent yet implemented solution with Linkedlist and hashing technique.

// Your code here along with comments explaining your approach
// I have used BitArray since it slightly saves your memory than bool[] ,
// if the size of the data is unknown then array is not the feasible option
// we should be using linkedlist and hashing techniques
//but to do that I need some time to understand that concept so I will take some time to implement that approach.


public class MyHashSet
{
//bool[] arr;
BitArray arr;
public MyHashSet()
{
// arr = new bool[1_000_001];
arr = new BitArray(1000001);
}

public void Add(int key)
{
arr[key] = true;
}

public void Remove(int key)
{
arr[key] = false;
}

public bool Contains(int key)
{
return arr[key];
}
}

/**
* 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);
*/

/**
* 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);
*/
7 changes: 0 additions & 7 deletions Sample.java

This file was deleted.