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

Complete design1 #2158

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
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions .idea/Design-1.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 59 additions & 0 deletions problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
public class problem1 {

// explanation: In this problem to implement hashset we consume the concept of double hash operation. Starting by creating 2d array
// and initializing the buckets sizes as 10^3 for, except primarybuckets due to test case issue . Caluating primaryhash by % operation
// and to calculate secondary hash by / operation . we do so to calculate the index for the key



class myHashSet{
int primaryBuckets;
int secondaryBuckets;
boolean[][] storage;
public myHashSet(){
this.primaryBuckets=1001;
this.secondaryBuckets=1000;
storage = new boolean[primaryBuckets][];
}
private int getPrimaryHash(int key)
{
return key%primaryBuckets;
}
private int getSecondaryHash(int key)
{
return key/secondaryBuckets;
}
public void add(int key)
{
int primaryIndex= getPrimaryHash(key);
if (storage[primaryIndex] == null)
{
storage[primaryIndex]= new boolean[secondaryBuckets];
}
int secondaryIndex= getSecondaryHash(key);
storage[primaryIndex][secondaryIndex]=true;
}
public void remove(int key)
{
int primaryIndex= getPrimaryHash(key);
if (storage[primaryIndex] == null)
{
return;
}
int secondaryIndex= getSecondaryHash(key);
storage[primaryIndex][secondaryIndex]=false;
}
public boolean contains(int key)
{
int primaryIndex= getPrimaryHash(key);
if (storage[primaryIndex] == null)
{
return false;
}
int secondaryIndex= getSecondaryHash(key);
return storage[primaryIndex][secondaryIndex];
}

}

}
35 changes: 35 additions & 0 deletions problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import java.util.Stack;

public class problem2 {

class MinStack{
Stack<Integer> st;
Stack<Integer> minSt;
int min;

public MinStack(){
this.st = new Stack<>();
this.minSt = new Stack<>();
min= Integer.MAX_VALUE;
minSt.push(min);
}
public void push(int x){
min= Math.min(min, x);
st.push(x);
minSt.push(min);
}
public void pop(){
st.pop();
minSt.pop();
min= st.peek();
}
public int top(){
return st.peek();
}
public int getMin(){
return min;
}
}


}