From 1c3108827d9bed63b80bf184f0d86df99ca858fa Mon Sep 17 00:00:00 2001 From: RamKuppagiri Date: Tue, 23 Dec 2025 18:42:50 -0600 Subject: [PATCH 1/5] hashset design --- hashSetDesign.js | 65 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 hashSetDesign.js diff --git a/hashSetDesign.js b/hashSetDesign.js new file mode 100644 index 00000000..5fc8d772 --- /dev/null +++ b/hashSetDesign.js @@ -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) + */ \ No newline at end of file From a10ceb79b2b5a2eda89a5fa1c36bc388083d59cf Mon Sep 17 00:00:00 2001 From: RamKuppagiri Date: Wed, 24 Dec 2025 19:29:49 -0600 Subject: [PATCH 2/5] minStack --- minStackDesign.js | 66 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 minStackDesign.js diff --git a/minStackDesign.js b/minStackDesign.js new file mode 100644 index 00000000..0b245d3c --- /dev/null +++ b/minStackDesign.js @@ -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() + */ \ No newline at end of file From 307d1112cbc4e29af5a0d37ffd48b145954c415c Mon Sep 17 00:00:00 2001 From: RamKuppagiri Date: Sat, 27 Dec 2025 21:56:25 -0600 Subject: [PATCH 3/5] search 2d matrix solution added --- hashSetDesign.js | 65 --------------------------------------------- minStackDesign.js | 66 ---------------------------------------------- searchA2DMatrix.js | 34 ++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 131 deletions(-) delete mode 100644 hashSetDesign.js delete mode 100644 minStackDesign.js create mode 100644 searchA2DMatrix.js diff --git a/hashSetDesign.js b/hashSetDesign.js deleted file mode 100644 index 5fc8d772..00000000 --- a/hashSetDesign.js +++ /dev/null @@ -1,65 +0,0 @@ - -/** - * 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) - */ \ No newline at end of file diff --git a/minStackDesign.js b/minStackDesign.js deleted file mode 100644 index 0b245d3c..00000000 --- a/minStackDesign.js +++ /dev/null @@ -1,66 +0,0 @@ - -/** - * 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() - */ \ No newline at end of file diff --git a/searchA2DMatrix.js b/searchA2DMatrix.js new file mode 100644 index 00000000..e75b8dcf --- /dev/null +++ b/searchA2DMatrix.js @@ -0,0 +1,34 @@ +/** + * @param {number[][]} matrix + * @param {number} target + * @return {boolean} + * + * Intution: + * Here we are imagining a 2d matrix as an array and performing the binary search + * I set the left to the first index and set the right to the last index + * Inside the while we are deriving the row col based on two formulas provided in the code, and performing the binary search. + */ +var searchMatrix = function(matrix, target) { + // Set left to the first element of the array + let left = 0; + const totalCols = matrix[0].length; + // Set right to the last element of the array + let right = (matrix.length * matrix[0].length) - 1; + + while(left <= right) { + const mid = Math.floor(left + (right-left)/2); + // formula to find the row number + const row = Math.floor(mid / totalCols); + // formula to find the col number + const col = Math.floor(mid % totalCols); + const currVal = matrix[row][col]; + if(currVal === target) { + return true; + }else if(currVal < target) { + left = mid + 1; + }else { + right = mid - 1; + } + } + return false; +}; \ No newline at end of file From 7af11268f7921f9cbfeaff14ddcbf7ea11b21b11 Mon Sep 17 00:00:00 2001 From: RamKuppagiri Date: Sat, 27 Dec 2025 21:57:16 -0600 Subject: [PATCH 4/5] comments added --- searchA2DMatrix.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/searchA2DMatrix.js b/searchA2DMatrix.js index e75b8dcf..aaaea5c7 100644 --- a/searchA2DMatrix.js +++ b/searchA2DMatrix.js @@ -23,6 +23,7 @@ var searchMatrix = function(matrix, target) { const col = Math.floor(mid % totalCols); const currVal = matrix[row][col]; if(currVal === target) { + // return true if element is found return true; }else if(currVal < target) { left = mid + 1; @@ -30,5 +31,6 @@ var searchMatrix = function(matrix, target) { right = mid - 1; } } + // return false if nothing is found return false; }; \ No newline at end of file From 07786c2098d0ed47bf3ab3ee7c739d6ee8db444f Mon Sep 17 00:00:00 2001 From: RamKuppagiri Date: Sat, 27 Dec 2025 21:58:46 -0600 Subject: [PATCH 5/5] comments updated --- searchA2DMatrix.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/searchA2DMatrix.js b/searchA2DMatrix.js index aaaea5c7..1623b527 100644 --- a/searchA2DMatrix.js +++ b/searchA2DMatrix.js @@ -6,7 +6,7 @@ * Intution: * Here we are imagining a 2d matrix as an array and performing the binary search * I set the left to the first index and set the right to the last index - * Inside the while we are deriving the row col based on two formulas provided in the code, and performing the binary search. + * Inside the while loop, we are deriving the row col based on two formulas, and performing the binary search. */ var searchMatrix = function(matrix, target) { // Set left to the first element of the array