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
78 changes: 78 additions & 0 deletions hashset.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//DESIGNING A HASH SET



class MyHashSet {
private:
int primary_buckets;
int secondary_buckets;
std::vector<std::vector<bool>*> storage;

int getPrimaryhash(int key){
return key % primary_buckets;
//tells us which bucket to choose
//makes sure the value is always between 0 and 999
}
int getSecondaryhash(int key){
return key / primary_buckets;
// gives us the position inside the bucket
}

public:
MyHashSet() {
primary_buckets = 1000;
secondary_buckets = 1000;
// memory set between 0 and 10^6 inclusive,hence 10^3 is the most optimal choice
storage = std::vector<std::vector<bool>*>(primary_buckets,nullptr);
//initially all the buckets are set to null and only created when needed
}

void add(int key) {
int primaryindex = getPrimaryhash(key);
if(storage[primaryindex] == nullptr){
if(primaryindex == 0){
storage[primaryindex] = new std::vector<bool>(secondary_buckets + 1,false);
//edge case for index 0 where 1001 buckets need to be allotted
//false here indicates that secondary bucket has been created
}

else{
storage[primaryindex] = new std::vector<bool>(secondary_buckets,false);
// for rest all positions we keep it as 1000
//false here indicates that secondary bucket has been created
}
}
int secondaryindex = getSecondaryhash(key);
(*storage[primaryindex])[secondaryindex] = true;
//indicates that the key is stored in the given position
}

void remove(int key) {
int primaryindex = getPrimaryhash(key);
if(storage[primaryindex] == nullptr)return;
//direct return as nothing exists at primary index
int secondaryindex = getSecondaryhash(key);
(*storage[primaryindex])[secondaryindex] = false;
//value is removed so we change it to false
}

bool contains(int key) {
int primaryindex = getPrimaryhash(key);
if(storage[primaryindex] == nullptr)return false;
int secondaryindex = getSecondaryhash(key);
return (*storage[primaryindex])[secondaryindex];
}
~MyHashSet() {
for (auto ptr : storage) {
delete ptr;
}
}
};

/**
* Your MyHashSet object will be instantiated and called as such:
* MyHashSet* obj = new MyHashSet();
* obj->add(key);
* obj->remove(key);
* bool param_3 = obj->contains(key);
*/
48 changes: 48 additions & 0 deletions minstack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//MIN-STACK

class MinStack {
stack<int> st;
//actual input stack
stack<int>minSt;
int minVal;
//variable for tracking minimum value
public:
MinStack() {
minVal = INT_MAX;
//in the start, no min value exists so the largest integer is allocated as the minimum
minSt.push(minVal);
}

void push(int val) {
minVal = std::min(val,minVal);
//existing minVal and val are compared and then the minVal is updated is val < minVal
st.push(val);
minSt.push(minVal);
}

void pop() {
//one to one mapping for every element pushed in actual stack is done wrt to min stack
st.pop();
minSt.pop();
// when one element is popped from actual stack, the corresponding minVal in also popped from min stack
minVal = minSt.top();
// after popping from min stack,the left out value is the new min value
}

int top() {
return st.top();
}

int getMin() {
return minVal;
}
};

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