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

Design-1 #2288

Open
wants to merge 2 commits 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
99 changes: 99 additions & 0 deletions HashSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Time Complexity : add, remove, contains = O(1)
// Space Complexity : O(n * m) where n = buckets, m = bucketItems
// 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
class MyHashSet {

//If element is present then it is true else false
private boolean[][] storage;
//size of array
private int buckets;
//size of nested array
private int bucketItems;

//Hash 1
public int bucket(int key) {
//we get location in array
return key % buckets;
}

//Hash 2 :- input will never have same hash1 and hash2 hence no collision
public int bucketItem(int key) {
//we get location in nested array
return key / bucketItems;
}

public MyHashSet() {

//we take square root of 10^6 which is range of inputs
this.buckets = 1000;
this.bucketItems = 1000;

//We do not give nested array size because if there is no element in that index we will not create the nested array thereby saving space
storage = new boolean[buckets][];
}

public void add(int key) {

int location = bucket(key);

//if the array location is empty we will create nested array and add the element
if(storage[location] == null) {

if(location == 0) {

//We do +1 here because for eg:- if the input is 10^6. The size of array is 1000 which is 0 to 999 and nested array is also 0-999. but if input is 10^6, we wont be able to store the value in nested array as 10^6 / 1000 = 1000.
storage[location] = new boolean[bucketItems + 1];

}else {
storage[location] = new boolean[bucketItems];
}
}

//location in the nested array
int nesLocation = bucketItem(key);
storage[location][nesLocation] = true;
}

public void remove(int key) {

int location = bucket(key);

//It means this element is not present in hash set
if(storage[location] == null)
return;

//If it is present we will find the exact location in nested array
int nesLocation = bucketItem(key);

//If there was an element in the index it would be true
storage[location][nesLocation] = false;

}

public boolean contains(int key) {

int location = bucket(key);

//It means this element is not present in hash set
if(storage[location] == null)
return false;

//Find the exact location of key
int nesLocation = bucketItem(key);

//If there is an element it is true else false
return storage[location][nesLocation];
}
}

/**
* 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);
*/
65 changes: 65 additions & 0 deletions MinStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Time Complexity : Push = O(1), Pop = O(1), top = O(1), getMin = O(1)
// Space Complexity : O(n) where n = 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
class MinStack {

int min;
Stack<Integer> stack;
Stack<Integer> minStack;

public MinStack() {

this.min = Integer.MAX_VALUE;
stack = new Stack<>();
minStack = new Stack<>();
}

public void push(int val) {

//If we get a new value which is smaller than current min
//We will save it as current min. But we also save previous min because we need it if we pop current min in future
//Even if new val == min we save it
if(min >= val) {
minStack.push(min);
min = val;
}

stack.push(val);
}

public void pop() {

int popped = stack.pop();

//If we are popping the minimum value then our minimum will be previous min
if(popped == min) {
//Store prev min in min
min = minStack.pop();
}
}

public int top() {

//get the top element from stack
return stack.peek();
}

public int getMin() {

//we store current minimum in min
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();
*/
7 changes: 0 additions & 7 deletions Sample.java

This file was deleted.