From fb314a636fb2d26e8cc3e4c2d2004ffd671dbfd2 Mon Sep 17 00:00:00 2001 From: Malav Soni Date: Wed, 23 Oct 2024 22:45:30 +0530 Subject: [PATCH 1/3] Design 2 Completed --- hash_map.js | 92 +++++++++++++++++++++++++++++++++++++++++++++ queue_with_stack.js | 62 ++++++++++++++++++++++++++++++ 2 files changed, 154 insertions(+) create mode 100644 hash_map.js create mode 100644 queue_with_stack.js diff --git a/hash_map.js b/hash_map.js new file mode 100644 index 00000000..9e936ea3 --- /dev/null +++ b/hash_map.js @@ -0,0 +1,92 @@ +// Time Complexity : put, get, remove O(1) +// Space Complexity : O(1) +// 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 + +class Node { + constructor(key, value) { + this.key = key; + this.value = value; + this.next = null; + } +} + +var MyHashMap = function () { + this.bucket = 10000; + this.storage = new Array(this.bucket).fill(null); +}; + +/** + * @param {number} key + * @param {number} value + * @return {void} + */ +MyHashMap.prototype.put = function (key, value) { + let primaryIndex = this.getPrimaryHash(key); + if (this.storage[primaryIndex] == null) { + this.storage[primaryIndex] = new Node(-1, -1); + } + let previousNode = this.getPreviousNode(this.storage[primaryIndex], key); + if (previousNode.next == null) { + previousNode.next = new Node(key, value); + } else { + previousNode.next.value = value; + } +}; + +/** + * @param {number} key + * @return {number} + */ +MyHashMap.prototype.get = function (key) { + let primaryIndex = this.getPrimaryHash(key); + if (this.storage[primaryIndex] == null) { + return -1; + } + let previousNode = this.getPreviousNode(this.storage[primaryIndex], key); + if (previousNode.next == null) { + return -1; + } else { + return previousNode.next.value; + } +}; + +/** + * @param {number} key + * @return {void} + */ +MyHashMap.prototype.remove = function (key) { + let primaryIndex = this.getPrimaryHash(key); + if (this.storage[primaryIndex] == null) { + this.storage[primaryIndex] = new Node(-1, -1); + } + let previousNode = this.getPreviousNode(this.storage[primaryIndex], key); + if (previousNode.next == null) { + return -1; + } else { + previousNode.next = previousNode.next.next; + } +}; + +MyHashMap.prototype.getPreviousNode = function (node, key) { + previousNode = null; + currentNode = node; + while (currentNode != null && currentNode.key != key) { + previousNode = currentNode; + currentNode = currentNode.next; + } + return previousNode; +}; + +MyHashMap.prototype.getPrimaryHash = function (key) { + return key % this.bucket; +}; + +// * Your MyHashMap object will be instantiated and called as such: +var obj = new MyHashMap(); +obj.put(1, 1); +obj.put(2, 2); +console.log(obj.get(1)); +obj.remove(5); diff --git a/queue_with_stack.js b/queue_with_stack.js new file mode 100644 index 00000000..496bcccd --- /dev/null +++ b/queue_with_stack.js @@ -0,0 +1,62 @@ +// Time Complexity : Push, Pop, Peek O(1) +// Space Complexity : O(1) +// 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 + +var MyQueue = function () { + this.in_stack = []; + this.out_stack = []; +}; + +/** + * @param {number} x + * @return {void} + */ +MyQueue.prototype.push = function (x) { + this.in_stack.push(x); +}; + +/** + * @return {number} + */ +MyQueue.prototype.pop = function () { + if (this.out_stack.length == 0) { + this.transfer(); + } + return this.out_stack.pop(); +}; + +/** + * @return {number} + */ +MyQueue.prototype.peek = function () { + if (this.out_stack.length == 0) { + this.transfer(); + } + return this.out_stack[this.out_stack.length - 1]; +}; + +MyQueue.prototype.transfer = function () { + while (this.in_stack.length != 0) { + this.out_stack.push(this.in_stack.pop()); + } +}; + +/** + * @return {boolean} + */ +MyQueue.prototype.empty = function () { + return this.in_stack.length == 0 && this.out_stack.length == 0; +}; + +/** + * Your MyQueue object will be instantiated and called as such: + * var obj = new MyQueue() + * obj.push(x) + * var param_2 = obj.pop() + * var param_3 = obj.peek() + * var param_4 = obj.empty() + */ From b000d5a5e3a76abb5c00a2f6c116f69c9edaec4b Mon Sep 17 00:00:00 2001 From: Malav Soni Date: Sat, 26 Oct 2024 14:53:14 +0530 Subject: [PATCH 2/3] tests added --- hash_map.js | 14 +++++++++----- queue_with_stack.js | 13 ++++++++++++- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/hash_map.js b/hash_map.js index 9e936ea3..a1c57ac2 100644 --- a/hash_map.js +++ b/hash_map.js @@ -85,8 +85,12 @@ MyHashMap.prototype.getPrimaryHash = function (key) { }; // * Your MyHashMap object will be instantiated and called as such: -var obj = new MyHashMap(); -obj.put(1, 1); -obj.put(2, 2); -console.log(obj.get(1)); -obj.remove(5); + +test("Scenario #1:", () => { + var obj = new MyHashMap(); + obj.put(1, 1); + obj.put(2, 2); + + expect(obj.get(1)).toStrictEqual(1); + expect(obj.remove(5)).toStrictEqual(-1); +}); diff --git a/queue_with_stack.js b/queue_with_stack.js index 496bcccd..eb640b00 100644 --- a/queue_with_stack.js +++ b/queue_with_stack.js @@ -3,7 +3,6 @@ // 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 var MyQueue = function () { @@ -60,3 +59,15 @@ MyQueue.prototype.empty = function () { * var param_3 = obj.peek() * var param_4 = obj.empty() */ + +test("Scenario #1:", () => { + var obj = new MyQueue(); + obj.push(1); + obj.push(2); + obj.push(3); + obj.push(4); + + expect(obj.pop()).toStrictEqual(1); + expect(obj.peek()).toStrictEqual(2); + expect(obj.empty()).toStrictEqual(false); +}); From 465508c891f6b10313420105c21d377390d90211 Mon Sep 17 00:00:00 2001 From: Malav Soni Date: Sun, 27 Oct 2024 21:45:11 +0530 Subject: [PATCH 3/3] Removed sample file --- Sample.java | 7 ------- hash_map.js | 4 ++++ 2 files changed, 4 insertions(+), 7 deletions(-) delete mode 100644 Sample.java diff --git a/Sample.java b/Sample.java deleted file mode 100644 index 1739a9cb..00000000 --- a/Sample.java +++ /dev/null @@ -1,7 +0,0 @@ -// Time Complexity : -// Space Complexity : -// Did this code successfully run on Leetcode : -// Any problem you faced while coding this : - - -// Your code here along with comments explaining your approach diff --git a/hash_map.js b/hash_map.js index a1c57ac2..5742ff15 100644 --- a/hash_map.js +++ b/hash_map.js @@ -5,6 +5,8 @@ // Your code here along with comments explaining your approach +// Collisan + class Node { constructor(key, value) { this.key = key; @@ -70,6 +72,8 @@ MyHashMap.prototype.remove = function (key) { } }; +// Why do we use previous node instead of current? +// Ans. For delete operation we need to know the previous node and we do not store previous pointer in node class. MyHashMap.prototype.getPreviousNode = function (node, key) { previousNode = null; currentNode = node;