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
50 changes: 50 additions & 0 deletions Design-hashset.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
class MyHashSet {
private boolean[][] storage;
private int buckets; //primary index of primary array
private int bucketItems; //secondary index of seconday array
public MyHashSet() {
this.buckets = 1000;
this.bucketItems = 1000;
this.storage = new boolean[buckets][];

}
private int hash1(int key){
return key % this.buckets;
}
private int hash2(int key){
return key / this.bucketItems;
}
public void add(int key) {
int bucket = hash1(key);
int bucketItem = hash2(key);
if(storage[bucket] == null){
if(bucket == 0)
storage[bucket] = new boolean[bucketItems+1]; //there are 1M+1 items for bucket 0
else
storage[bucket] = new boolean[bucketItems];
}
storage[bucket][bucketItem] = true;
}

public void remove(int key) {
int bucket = hash1(key);
int bucketItem = hash2(key);
if(storage[bucket] == null) return;
storage[bucket][bucketItem] = false;
}

public boolean contains(int key) {
int bucket = hash1(key);
int bucketItem = hash2(key);
if(storage[bucket] == null) return false;
return storage[bucket][bucketItem];
}
}

/**
* Your MyHashSet object will be instantiated and called as such:
* MyHashSet obj = new MyHashSet();
* obj.add(key);
* obj.remove(key);
* boolean param_3 = obj.contains(key);
*/
40 changes: 40 additions & 0 deletions Min-stack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class MinStack {
Stack<Integer> st;
Stack<Integer> minSt;
int min;
public MinStack() {
this.st = new Stack<>();
this.minSt = new Stack<>();
this.min = Integer.MAX_VALUE;
this.minSt.push(min);
}

public void push(int val) {
min = Math.min(min, val);
st.push(val);
minSt.push(min);
}

public void pop() {
st.pop();
minSt.pop();
min = minSt.peek(); //dont forget to reset min once pop happens
}

public int top() {
return st.peek();
}

public int getMin() {
return min;
}
}

/**
* 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();
*/
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ Explain your approach in **three sentences only** at top of your code
# Design-1

## Problem 1:(https://leetcode.com/problems/design-hashset/)


Need to design Hashset w/o in-built libraries. Implement add(), contains(), remove().
Use double hashing technique in a primary array of references, initialize secondary array of boolean values
Don't forget that that there is 1M+1 elements for which extra bucket element will be inserted

## Problem 2:
Design MinStack (https://leetcode.com/problems/min-stack/)
Maintain 2 stacks - stack , minStack.
minStack will always have a MAX_VALUE. dont forget to reset min once pop happens bcz once element is popped it cannot be the min.