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

Completed Design-1 #2293

Open
wants to merge 1 commit 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
64 changes: 64 additions & 0 deletions Problem-1
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// https://leetcode.com/problems/design-hashset/description/
// Time Complexity : O(1)
// Space Complexity : O(n)
// 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
// Used double hashing method as taught in the class


class MyHashSet {
public:
vector<vector<bool>*> arr;
int buckets;
int bucketList;
int hash1(int key){
return key%buckets;
}
int hash2(int key){
return key/bucketList;
}
MyHashSet() {
buckets = 1000;
bucketList = 1000;
arr.resize(buckets,NULL);
}

void add(int key) {
int buck = hash1(key);
if(arr[buck] == NULL){
if(buck == 0){
arr[buck] = new vector<bool>(bucketList+1, false);
}
else{
arr[buck] = new vector<bool>(bucketList, false);
}
}
int buckList = hash2(key);
(*arr[buck])[buckList] = true;
}

void remove(int key) {
int buck = hash1(key);
if(arr[buck] == NULL) return;
int buckList = hash2(key);
(*arr[buck])[buckList] = false;
}

bool contains(int key) {
int buck = hash1(key);
if(arr[buck] == NULL) return false;
int buckList = hash2(key);
return (*arr[buck])[buckList];
}
};

/**
* 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);
*/
63 changes: 63 additions & 0 deletions Problem-2
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//https://leetcode.com/problems/min-stack/
// Time Complexity : O(1)
// push(): O(1)
// pop(): O(1)
// top(): O(1)
// getMin(): O(1)
// Space Complexity : O(n) where n is the 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
// Declare an extra stack to store min value so far.
// Initialize the min stack with maximum value, as elements are pushed, check the min value so far
// update it if the current element pushed is smaller than the min value or else continue.




class MinStack {
public:
stack<int> st;
int minVal = INT_MAX;
stack<int> minSt;

MinStack() {
minSt.push(minVal);
}

void push(int val) {
if(minVal>=val){
minSt.push(val); //stores val only if it's smaller than the existing min value.
minVal = val;
}
st.push(val);
}

void pop() {
int popped = st.top();
st.pop();
if(popped == minVal){
minSt.pop();
minVal = minSt.top();
}
}

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