Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Done Design-1: MinStack and MyHashSet #2279

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
55 changes: 55 additions & 0 deletions MinStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Time Complexity : O(1) for all operations
// Space Complexity : O(n) where n is the number of elements in the stack
// Did this code successfully run on Leetcode : Yes
// Approach: I am using 2 stacks. One stack is used to store the elements and the other stack is used to store the minimum element at that point. I am pushing the element to the stack and also pushing the minimum element to the minStack. If the element is removed from the stack, I am removing the element from the stack and also removing the element from the minStack. If the minStack is empty, I am setting the min value to Integer.MAX_VALUE. If the minStack is not empty, I am setting the min value to the top element of the minStack.

import java.util.Stack;

class MinStack {

Stack<Integer> stack;
Stack<Integer> minStack;
int min = Integer.MAX_VALUE;

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

public void push(int val) {
min = Math.min(val, min);
stack.push(val);
minStack.push(min);
}

public void pop() {
if(!stack.isEmpty()){
stack.pop();
}
if(!minStack.isEmpty()){
minStack.pop();
}
if(!minStack.isEmpty()){
min = minStack.peek();
}else{
min = Integer.MAX_VALUE;
}
}

public int top() {
return stack.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();
*/
68 changes: 68 additions & 0 deletions MyHashSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Time Complexity : O(1) for all operations
// Space Complexity : O(n) where n is the number of elements in the hashset
// Did this code successfully run on Leetcode : Yes
// Approach: I am using 2D array where primary array is of size 1000 and secondary array is of size 1000. I am using 2 hash functions to calculate the index of the primary and secondary array. I am using boolean values to store the values in the hashset. If the value is present in the hashset, I am setting the value to true. If the value is removed from the hashset, I am setting the value to false. If the value is not present in the hashset, I am returning false.

class MyHashSet {

boolean[][] values;
int buckets;
int bucketItems;

public MyHashSet() {
buckets = 1000;
bucketItems = 1000;
values = new boolean[buckets][];
}

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

private int hash2(int key){
return key/bucketItems;
}

public void add(int key) {
int index1 = hash1(key);
int index2 = hash2(key);

if(values[index1] == null){
if(index1 == 0){
values[index1] = new boolean[bucketItems+1];
}else{
values[index1] = new boolean[bucketItems];
}
}
values[index1][index2] = true;
}

public void remove(int key) {
int index1 = hash1(key);
int index2 = hash2(key);

if(values[index1] == null){
return;
}else{
values[index1][index2] = false;
}
}

public boolean contains(int key) {
int index1 = hash1(key);
int index2 = hash2(key);

if(values[index1] == null){
return false;
}
return values[index1][index2];
}
}

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