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 #2172

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
114 changes: 110 additions & 4 deletions Sample.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,113 @@
// Time Complexity :
// Space Complexity :
// Did this code successfully run on Leetcode :
// Any problem you faced while coding this :
//Problem-1 Hashset
// Time Complexity : O(1) for adding, deleting, contains
// Space Complexity :
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this : Getting partial testcases correct for assigning 1 extra bucket for
// secondary hash function. So added 1001 buckets in the global declaration of secondary buckets.


class MyHashSet {
int primaryList;
int secondaryList;
boolean [][] storage;
public MyHashSet() {
this.primaryList=1000;
this.secondaryList=1001;
this.storage = new boolean[primaryList][];
}
private int primaryhash(int key)
{
return key % primaryList;
}
private int secondaryhash(int key)
{
return key / secondaryList;
}
public void add(int key) {
int primarykey= primaryhash(key);
if(storage[primarykey]==null)
{

storage[primarykey]= new boolean[secondaryList+1];
}
int secondarykey=secondaryhash(key);
storage[primarykey][secondarykey]=true;

}

public void remove(int key) {
int primarykey= primaryhash(key);
if(storage[primarykey]==null)
{
return;
}
else
{
int secondarykey=secondaryhash(key);
storage[primarykey][secondarykey]=false;
}

}

public boolean contains(int key) {
int primarykey= primaryhash(key);
if(storage[primarykey]==null)
{
return false;
}
else
{
int secondarykey=secondaryhash(key);
return storage[primarykey][secondarykey];
}

}
}

// Uses double hashing with primary hashing using a modulous and secondary hashing using a division operation.
// took 1000 buckets in each hashing by Sq. root of 10^6.
// used boolean instead of integers.

// Your code here along with comments explaining your approach


//Problem-2 Min Stack
// Time Complexity : O(1) for push, pop as operations on stack is O(1)
// Space Complexity :
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this :

class MinStack {
Stack<Integer> stack;
Stack<Integer> minStack;
int min;
public MinStack() {
this.stack = new Stack<>();
this.minStack = new Stack<>();
min=Integer.MAX_VALUE;
minStack.push(min);
}

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

public void pop() {
stack.pop();
minStack.pop();
min=minStack.peek();

}

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

public int getMin() {
return min;
}
}