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
65 changes: 65 additions & 0 deletions hashSetDesign.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@

/**
* Intution:
* I am using bucket + arrays concept that i learnt online,
* Using hash function to give an index for the given key which helps us identifying the bucket during add/ remove operations
* chaining (each index holds an array) helps us having store all the values that belongs to that hash index without conflicts.

*/

class MyHashSet {
constructor() {

this.numerOfBuckets = 1000;
this.buckets = Array.from({
length: this.numerOfBuckets
},
() => []);
}

hashing = (key) => {
return key % this.numerOfBuckets;
}
};


/**
* @param {number} key
* @return {void}
*/
MyHashSet.prototype.add = function (key) {
const hashIndex = this.hashing(key);
const currBucket = this.buckets[hashIndex];
if (!currBucket?.includes(key))
currBucket.push(key);
};

/**
* @param {number} key
* @return {void}
*/
MyHashSet.prototype.remove = function (key) {
const hashIndex = this.hashing(key);
const currBucket = this.buckets[hashIndex];
if (currBucket.includes(key)) {
const indexOfTheKey = currBucket.indexOf(key);
currBucket.splice(indexOfTheKey, 1);
}
};

/**
* @param {number} key
* @return {boolean}
*/
MyHashSet.prototype.contains = function (key) {
const hashIndex = this.hashing(key);
return this.buckets[hashIndex].includes(key);
};

/**
* Your MyHashSet object will be instantiated and called as such:
* var obj = new MyHashSet()
* obj.add(key)
* obj.remove(key)
* var param_3 = obj.contains(key)
*/
66 changes: 66 additions & 0 deletions minStackDesign.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@

/**
* I am using two stacks here
* One to track the values that we are pushing and popping, The other one is to track the minVal at that point of time.
* Reduced more space by pushing the minVal into minStack if and only if the current val that we are pushing into stack is less than the minStack peek val.
*
*/

class MinStack {
constructor(){
this.minVal = Infinity;
this.stack = [];
this.minStack = [];
this.minStack.push(this.minVal);
}
};

/**
* @param {number} val
* @return {void}
*/
MinStack.prototype.push = function(val) {
// compare the incoming value with the minVal and update the minVal if needed
if(val <= this.minVal){
this.minVal = val;
// push the minVal on the minStack
this.minStack.push(val);
}
// push the val on to stack
this.stack.push(val);
};

/**
* @return {void}
*/
MinStack.prototype.pop = function() {
const currVal = this.stack.pop();
if(currVal === this.getMin()){
this.minStack.pop();
}
this.minVal = this.minStack[this.minStack.length - 1];
return currVal;
};

/**
* @return {number}
*/
MinStack.prototype.top = function() {
return this.stack[this.stack.length - 1];
};

/**
* @return {number}
*/
MinStack.prototype.getMin = function() {
return this.minStack[this.minStack.length - 1];
};

/**
* Your MinStack object will be instantiated and called as such:
* var obj = new MinStack()
* obj.push(val)
* obj.pop()
* var param_3 = obj.top()
* var param_4 = obj.getMin()
*/