diff --git a/hashset.cpp b/hashset.cpp new file mode 100644 index 00000000..fae673d7 --- /dev/null +++ b/hashset.cpp @@ -0,0 +1,78 @@ +//DESIGNING A HASH SET + + + +class MyHashSet { + private: + int primary_buckets; + int secondary_buckets; + std::vector*> 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*>(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(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(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); + */ \ No newline at end of file diff --git a/minstack.cpp b/minstack.cpp new file mode 100644 index 00000000..f2a093f6 --- /dev/null +++ b/minstack.cpp @@ -0,0 +1,48 @@ +//MIN-STACK + +class MinStack { + stack st; + //actual input stack + stackminSt; + 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(); + */ \ No newline at end of file