From 1c3108827d9bed63b80bf184f0d86df99ca858fa Mon Sep 17 00:00:00 2001 From: RamKuppagiri Date: Tue, 23 Dec 2025 18:42:50 -0600 Subject: [PATCH 1/3] 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/3] 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 c52902119cab245fe65c87ab1926a952cbee9bd7 Mon Sep 17 00:00:00 2001 From: RamKuppagiri Date: Sun, 28 Dec 2025 11:32:40 -0600 Subject: [PATCH 3/3] LC 34 --- firstAndLastElementOfAnArr.js | 57 ++++++++++++++++++++++++++++++ hashSetDesign.js | 65 ---------------------------------- minStackDesign.js | 66 ----------------------------------- 3 files changed, 57 insertions(+), 131 deletions(-) create mode 100644 firstAndLastElementOfAnArr.js delete mode 100644 hashSetDesign.js delete mode 100644 minStackDesign.js diff --git a/firstAndLastElementOfAnArr.js b/firstAndLastElementOfAnArr.js new file mode 100644 index 00000000..f010fc8a --- /dev/null +++ b/firstAndLastElementOfAnArr.js @@ -0,0 +1,57 @@ +/** + * @param {number[]} nums + * @param {number} target + * @return {number[]} + * + * Intution: + * The approach here is using binary search twice to get start and end index of the target + * We are even returning the firstFound location, so that we can optimize the end index calculation + * Time complexity is O(log n) + O(log n) => O(log n) + * + */ + + +var searchRange = function (nums, target) { + let left = 0, right = nums.length - 1; + // Method to get the first occurence of the target + const firstLocBinarySearch = (low, high) => { + let firstFound = -1; + while (low <= high) { + const mid = Math.floor(low + (high - low) / 2); + if (nums[mid] === target) { + if (firstFound === -1) firstFound = mid; + if (mid === 0 || nums[mid] !== nums[mid - 1]) return [firstFound, mid]; + else high = mid - 1; + } else if (nums[mid] < target) { + low = mid + 1; + } else { + high = mid - 1; + } + } + return [-1, -1]; + } + + // Method to get the last occurence of the target + const lastLocBinarySearch = (low, high) => { + while (low <= high) { + const mid = Math.floor(low + (high - low) / 2); + if (nums[mid] === target) { + if (mid === nums.length - 1 || nums[mid] !== nums[mid + 1]) return mid; + else low = mid + 1; + } else if (nums[mid] < target) { + low = mid + 1; + } else { + high = mid - 1; + } + + } + + return -1; + } + // get the firstfound location and the startIndex + let [firstFound, startIndex] = firstLocBinarySearch(left, right); + // if firstFound is -1 then the element is not found, can ignore the remaining code and return [-1,-1] + if (firstFound === -1) return [-1, -1]; + let endIndex = lastLocBinarySearch(startIndex, right); + return [startIndex, endIndex]; +}; \ No newline at end of file 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