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
47 changes: 47 additions & 0 deletions MinStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Time Complexity : O(1) for all push pop top getMin
// Space Complexity : O(n) where n is the number of elements in the stack
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this : none

// Your code here along with comments explaining your approach
//used two stacks, one for storing elements and one for storing the minimum element at each position.
import java.util.Stack;
class MinStack
{
private Stack<Integer> stack;
private Stack<Integer> minStack;
Comment on lines +8 to +12

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

public void push(int val)
{
stack.push(val);
if (minStack.isEmpty() || 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();
}
Comment on lines +32 to +46
}
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ Explain your approach in **three sentences only** at top of your code



## Problem 2:
Design MinStack (https://leetcode.com/problems/min-stack/)
## Problem 2: Design MinStack (https://leetcode.com/problems/min-stack/)



53 changes: 53 additions & 0 deletions design_hashset.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Time Complexity : O(1) for add, remove and contains operations
// Space Complexity : O(n) where n is the number of elements in the hash set
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this : none

// Your code here along with comments explaining your approach
//used bucketing to split each key in two parts: key % 1000 gives the bucket index, and key / 1000 gives the position inside that bucket.

class MyHashSet {
private boolean[][] storage;
private int buckets;
private int bucketItems;

public MyHashSet() {
this.buckets = 1000;
this.bucketItems = 1001;
Comment on lines +15 to +16
this.storage = new boolean[buckets][];
}

private int hash(int key) {
return key % buckets;
}

private int pos(int key) {
return key / buckets;
}
Comment on lines +14 to +26

public void add(int key) {
int bucket = hash(key);
int bucketItem = pos(key);

if (storage[bucket] == null) {
storage[bucket] = new boolean[bucketItems];
}
storage[bucket][bucketItem] = true;
}

public void remove(int key) {
int bucket = hash(key);
int bucketItem = pos(key);

if (storage[bucket] != null) {
storage[bucket][bucketItem] = false;
}
}

public boolean contains(int key) {
int bucket = hash(key);
int bucketItem = pos(key);

return storage[bucket] != null && storage[bucket][bucketItem];
}
}