Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Design 2 Completed #2032

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
7 changes: 0 additions & 7 deletions Sample.java

This file was deleted.

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

// Collisan

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;
}
};

// 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;
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:

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);
});
73 changes: 73 additions & 0 deletions queue_with_stack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// 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()
*/

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);
});