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
42 changes: 42 additions & 0 deletions Design-Hashset.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Time Complexity : O(1) for add, remove and contains
// Space Complexity : O(n) where n is the number of elements in the hashset
// 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
/*
We can use a boolean array to implement the hashset.
The index of the array will represent the key and when we add the key to the array, we will set the value true
When the remove function is called, we will mark the array[key] as false
When the contains function is called, we will return the value of the array[key]

*/
class MyHashSet {
boolean[] array = new boolean[1000001];

public MyHashSet() {

}

public void add(int key) {
array[key] = true;

}

public void remove(int key) {
array[key]=false;
}

public boolean contains(int key) {
return array[key];
}
}

/**
* 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);
*/
77 changes: 77 additions & 0 deletions Hashset.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Time Complexity :o(1) for add, remove and contains
// Space Complexity : O(n) where n is the number of elements in the hashset
// 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

/*
We can use a 2D boolean array to implement the hashset.
The index of the first dimension will represent the row and the index of the second dimension will represent
the column. When we add the key to the array, we will set the value true at the index of the row and column.
When the remove function is called, we will mark the array[row][column] as false
When the contains function is called, we will return the value of the array[row][column]
*/

class MyHashSet {
boolean array[][] = new boolean[1000][];


public MyHashSet() {


}
public int row(int key){
return key%1000;

}

public int column(int key){
return key/1000;

}


public void add(int key) {
int row = row(key);
int column = column(key);
if(array[row]==null){
array[row] = new boolean[1001];
}
array[row][column] = true;

}

public void remove(int key) {
int row = row(key);
int column = column(key);
if( array[row] !=null){
array[row][column] = false;
}


}

public boolean contains(int key) {
boolean value = false;
int row = row(key);
int column = column(key);
if( array[row] !=null){
value = array[row][column];
}
if(value){
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);
*/
63 changes: 63 additions & 0 deletions Min-Stack-155.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@


// Time Complexity : O(1) for push, pop and top, O(n) for getMin where n is the number of elements in the stack
// 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 : No


// Your code here along with comments explaining your approach
/*
We can use an ArrayList to implement the min stack.
When the push function is called, we will add the element to the end of the arraylist.
When the pop function is called, we will remove the last element from the arraylist.
When the top function is called, we will return the last element from the arraylist.
When the getMin function is called, we will iterate through the arraylist and find the minimum element and return it.

*/

class MinStack {
ArrayList<Integer> al = new ArrayList<Integer>();

public MinStack() {

}

public void push(int val) {
al.add(al.size(), val);


}

public void pop() {
al.remove(al.size()-1);


}

public int top() {
int result = al.get(al.size()-1);
return result;

}

public int getMin() {
int min = al.get(0);
for(int i =1 ; i<al.size(); i++){
if(min > al.get(i)){
min = al.get(i);
}

}
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();
*/
59 changes: 59 additions & 0 deletions Minstack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Time Complexity : O(1) for push, pop, top and 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 : No


// Your code here along with comments explaining your approach
/**
* This is a min stack that supports push, pop, top, and retrieving the minimum element in constant time.
* We use two stacks to achieve this. The first stack is used to store the elements of the stack and the second stack is used to store the minimum elements of the stack.
*/
class MinStack {
Stack<Integer> stack = new Stack<>();
Stack<Integer> minStack = new Stack<>();

public MinStack() {


}
}

public void push(int val) {
stack.push(val);
if(!(minStack.isEmpty())){
minStack.push(Math.min(val,minStack.peek()));

}else{
minStack.push(val);
}

}

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