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 20ff0eec9836b04887092dcdcc4b592104cbb217 Mon Sep 17 00:00:00 2001 From: RamKuppagiri Date: Fri, 26 Dec 2025 19:38:58 -0600 Subject: [PATCH 3/5] rotated sorted array --- roatedSortedArrBinSearch.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 roatedSortedArrBinSearch.js diff --git a/roatedSortedArrBinSearch.js b/roatedSortedArrBinSearch.js new file mode 100644 index 00000000..b57291ca --- /dev/null +++ b/roatedSortedArrBinSearch.js @@ -0,0 +1,19 @@ +/** + * I am using binary search on this rotated sorted array + * Mid formula makes sure that integer over flow is not happening + * Checking if the left side of the array is sorted or not, if yes i am checking if the target is in the range, if yes high is moving to left, else low is moving to right; + * Else if the right side is sorted check if the target is in range and move the low and high accordingly. + */ +var search = function(nums, target) { + let low = 0, high = nums.length-1; + while(low <= high) { + const mid = Math.floor( + low + (high - low)/2 + ); + if(nums[mid] === target) return mid; + else if(nums[mid] >= nums[low]) { + nums[low] <= target && target < nums[mid] ? high = mid-1 : low = mid + 1; + } else nums[mid] < target && target <= nums[high] ? low = mid + 1 : high = mid-1; + } + return -1; +}; \ No newline at end of file From 347575394615f71e5d82306eb745958871667845 Mon Sep 17 00:00:00 2001 From: dbkuppagiri <32444173+dbkuppagiri@users.noreply.github.com> Date: Fri, 26 Dec 2025 19:47:04 -0600 Subject: [PATCH 4/5] Delete minStackDesign.js --- minStackDesign.js | 66 ----------------------------------------------- 1 file changed, 66 deletions(-) delete mode 100644 minStackDesign.js 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 From 4c5268d216e1cde5c01f6ba840571f49bf2d0569 Mon Sep 17 00:00:00 2001 From: dbkuppagiri <32444173+dbkuppagiri@users.noreply.github.com> Date: Fri, 26 Dec 2025 19:48:10 -0600 Subject: [PATCH 5/5] Delete hashSetDesign.js --- hashSetDesign.js | 65 ------------------------------------------------ 1 file changed, 65 deletions(-) delete mode 100644 hashSetDesign.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