Skip to content

Commit

Permalink
'updates'
Browse files Browse the repository at this point in the history
  • Loading branch information
b-hexsoul committed May 15, 2022
1 parent 0ff3aed commit 4e014a8
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 107 deletions.
15 changes: 15 additions & 0 deletions DS/HashTable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// A good hash function;
// 1. Easy to caluclate the hash value ( low time complexity );
// 2. Chance of collision is low
// 3. All possible values are utilized approx. equally. Try to distribute uniformly

// Simple Hash Function for strings
function stringHash(key, arrayLen) {
let total = 0;
for (let char of key) {
// map "a" to 1, "b" to 2, "c" to 3, etc.
let value = char.charCodeAt(0) - 96;
total = (total + value) % arrayLen;
}
return total;
}
159 changes: 80 additions & 79 deletions DS/SingleLinkedList.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
// Node
class Node {
constructor(value) {
this.value = value
this.next = null
this.value = value;
this.next = null;
}
}

Expand All @@ -26,95 +26,96 @@ class Node {

class SinglyLinkedList {
constructor() {
this.length = 0
this.head = this.tail = null
this.length = 0;
this.head = this.tail = null;
}

push(element) {
this.length++
if(!this.tail) {
return this.head = this.tail = new Node(element)
this.length++;
if (!this.tail) {
return (this.head = this.tail = new Node(element));
}

const oldTail = this.tail
this.tail = new Node(element)
oldTail.next = this.tail
const oldTail = this.tail;
this.tail = new Node(element);
oldTail.next = this.tail;

return this
return this;
}

// with a SinglyLinkedList you need to traverse through from the head
pop() {
if(!this.tail) {
return undefined
if (!this.tail) {
return undefined;
}

if(this.head === this.tail) {
let deleted = this.head
this.head = this.tail = null
this.length--
return deleted
if (this.head === this.tail) {
let deleted = this.head;
this.head = this.tail = null;
this.length--;
return deleted;
}

let currentNode = this.head
const removed = this.tail
let currentNode = this.head;
const removed = this.tail;

while(currentNode) {
while (currentNode) {
if (currentNode.next === this.tail) {
this.tail = currentNode
this.tail = currentNode;
this.tail.next = null;
}
currentNode = currentNode.next
currentNode = currentNode.next;
}
this.length--
return removed
this.length--;
return removed;
}

shift() {
if(!this.head) {
return undefined
if (!this.head) {
return undefined;
}

const removed = this.head
this.length--
const removed = this.head;
this.length--;

if(this.head === this.tail) {
this.head = this.tail = null
return removed
if (this.head === this.tail) {
this.head = this.tail = null;
return removed;
}

this.head = this.head.next
removed.next = null
return removed
this.head = this.head.next;
removed.next = null;
return removed;
}

unshift(element) {
this.length++
this.length++;
if (!this.head) {
return this.head = this.tail = new Node(element)
return (this.head = this.tail = new Node(element));
}

const oldHead = this.head
this.head = new Node(element)
this.head.next = oldHead
const oldHead = this.head;
this.head = new Node(element);
this.head.next = oldHead;
}

get(idx) {
if (idx < 0 || idx >= this.length) {
return null
}
let counter = 0
let currentNode = this.head
while(counter !== idx) {
currentNode = currentNode.next
counter++
return null;
}
let counter = 0;
let currentNode = this.head;
while (counter !== idx) {
currentNode = currentNode.next;
counter++;
}
return currentNode
return currentNode;
}

set(idx, value) {
const foundNode = this.get(idx);
if(foundNode) {
foundNode.value = value
if (foundNode) {
foundNode.value = value;
return true;
}

Expand All @@ -123,36 +124,36 @@ class SinglyLinkedList {

insert(idx, value) {
if (idx < 0 || idx > this.length) {
return false
return false;
}

if (idx === this.length) {
return this.push(value)
return this.push(value);
}

if (idx === 0) {
return this.unshift(value)
return this.unshift(value);
}

const pre = this.get(idx - 1);
const after = pre.next
const newNode = new Node(value)
newNode.next = after
pre.next = newNode
this.length++
const after = pre.next;
const newNode = new Node(value);
newNode.next = after;
pre.next = newNode;
this.length++;
}

remove(idx) {
if (idx < 0 || idx >= this.length) return undefined
if (idx === 0) return this.shift()
if (idx === this.length - 1) return this.pop()

const prev = this.get(idx - 1)
const removed = prev.next
const nextN = removed.next
prev.next = nextN
removed.next = null
return removed
if (idx < 0 || idx >= this.length) return undefined;
if (idx === 0) return this.shift();
if (idx === this.length - 1) return this.pop();

const prev = this.get(idx - 1);
const removed = prev.next;
const nextN = removed.next;
prev.next = nextN;
removed.next = null;
return removed;
}

reverse() {
Expand All @@ -161,20 +162,20 @@ class SinglyLinkedList {
this.tail = node;
let prev = null;

while(node) {
let next = node.next
node.next = prev
prev = node
node = next
while (node) {
let next = node.next;
node.next = prev;
prev = node;
node = next;
}

return this
return this;
}
}

const list = new SinglyLinkedList()
list.push('zero')
list.push('one')
list.push('two')
const list = new SinglyLinkedList();
list.push("zero");
list.push("one");
list.push("two");

console.log(list.reverse())
console.log(list.reverse());
4 changes: 3 additions & 1 deletion Sorting/comparison/mergeSort.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ function merge(arr1, arr2) {
}
}

console.log("newArr1", newArr);

while (i < arr1.length) {
newArr.push(arr1[i]);
i++;
Expand All @@ -33,7 +35,7 @@ function merge(arr1, arr2) {
newArr.push(arr2[j]);
j++;
}

console.log("newArr2", newArr);
return newArr;
}

Expand Down
3 changes: 2 additions & 1 deletion Sorting/comparison/selectionSort.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Selection Sort!
// Places small values into sorted position one at a time. Moving from beginning to the end. Swap position of minimum with beginning of array.
// Places small values into sorted position one at a time. Moving from beginning to the end.
// Swap position of minimum with beginning of array.
// Make sure to increase the start of the array by 1.
// Time Complexity - Best/Worst/Average of O(n^2)
// Space Complexity - O(1)
Expand Down
File renamed without changes.
26 changes: 0 additions & 26 deletions questions/mergeIntervals.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,3 @@
// Given an array of intervals where intervals[i] = [starti, endi]
// merge all overlapping intervals,
// and return an array of the non-overlapping intervals that cover all the intervals in the input.

const merge = function (intervals) {
if (intervals.length === 1) return intervals;

const sortedIntervals = intervals.sort((a, b) => {
return a[0] - b[0];
});

let result = [];

result.push(intervals[0]);

for (let i = 1; i < intervals.length; i++) {
let previous = result.pop();
let current = intervals[i];
// merge
if (previous[1] >= current[0]) {
result.push([previous[0], Math.max(previous[1], current[1])]);
} else {
result.push(previous);
result.push(current);
}
}

return result;
};

0 comments on commit 4e014a8

Please sign in to comment.