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 @@
//Approach :
//TC : O(1)
//SC : O(1)
class MyHashSet {
int primaryKeyLen = 1000;
int secondaryKeyLen = 1000;
boolean[][] set;

public MyHashSet() {
set = new boolean[1000][]; //SC: 10^3 = constant
}

public void add(int key) { //TC: O(1) // SC: 10^3= constant = O(1)
int z = key % primaryKeyLen;
if (set[z] == null) {
set[z] = new boolean[1000]; //SC: 10^3 = constant
}
int s = key / 1000;
set[z][s] = true;
}

public void remove(int key) { //TC: O(1), SC: O(1)
int z = key % 1000;
int s = key / 1000;
if (set[z] == null) {
return;
}
set[z][s] = false;
}

public boolean contains(int key) {//TC: O(1), SC:O(1)
int z = key % 1000;
int s = key / 1000;
if (set[z] == null) {
return false;
}
if (set[z][s] == true) {
return true;
}
return false;
}
}

/**
* 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);
*/
52 changes: 52 additions & 0 deletions minStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

import java.util.*;
//Approach:
//TC: O(1)
//SC: 2*O(N)

class MinStack {

Stack<Integer> st;
Stack<Integer> minSt;

public MinStack() {
st = new Stack();
minSt = new Stack();
minSt.push(Integer.MAX_VALUE);
}

public void push(int value) {

if (minSt.peek() >= value) {
minSt.push(value);

}
st.push(value);
}

public void pop() {

if (st.peek().equals(minSt.peek())) {
minSt.pop();
}
st.pop();

}

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

public int getMin() {
return minSt.peek();
}
}

/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(value);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/