diff --git a/Chapter1/1.6-MatrixRotation.js b/Chapter1/1.6-MatrixRotation.js new file mode 100644 index 0000000..6c631ec --- /dev/null +++ b/Chapter1/1.6-MatrixRotation.js @@ -0,0 +1,31 @@ +/* +Problem: Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, +write a method to rotate the image by 90 degrees. +Can you do this in place? +*/ + +/* +ex: input [[0,1,0], [0,1,0], [0,0,0]] returns [ [ 0, 0, 0 ], [ 0, 1, 1 ], [ 0, 0, 0 ] ] +*/ + +function rotateNinety(matrix) { + const N = matrix.length; + var floor = Math.floor(N/2); + var ceil = Math.ceil(N/2); + x = 0; + y = 0; + while (x < floor) { + while(y < ceil) { + let temp = matrix[x][y]; + matrix[x][y] = matrix[N-y-1][x]; + matrix[N-y-1][x] = matrix[N-x-1][N-y-1]; + matrix[N-x-1][N-y-1] = matrix[y][N - x-1]; + matrix[y][N - x-1] = temp; + y++; + } + x++; + } + return matrix; +} + +//runtime: O(n^2) \ No newline at end of file diff --git a/Chapter1/1.7-ClearMatrix.js b/Chapter1/1.7-ClearMatrix.js new file mode 100644 index 0000000..32a9a5a --- /dev/null +++ b/Chapter1/1.7-ClearMatrix.js @@ -0,0 +1,39 @@ +/* +Problem: Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column are set to 0. +*/ + +/* +ex: input [ [1 ,2, 3], [0, 0, 4], [5, 6, 7] ] returns [ [ 0, 0, 3 ], [ 0, 0, 0 ], [ 0, 0, 7 ] ] +*/ + +function clearZeroes(matrix) { + //create empty row to use later on + var emptyRow = matrix[0].map(function(){ + return 0; + }) + + //store columns to check if column has already been cleared + var emptyColumnArray = []; + + var emptyColumns = function(index) { + emptyColumnArray.push(index); + matrix.forEach(function(row, i) { + matrix[i][index] = 0; + }) + } + + matrix.forEach((row,j) => { + for (let i = 0, len = row.length; i < len; i++) { + if (row[i] === 0 && emptyColumnArray.indexOf(i) === -1) { + emptyColumns(i); + matrix[j] = emptyRow; + //break out of loop since we already cleared the entire row + i = -1; + } + } + }) + + return matrix; +} + +//runtime: O(n^2) diff --git a/Chapter17-Moderate/swapNumbers.js b/Chapter17-Moderate/swapNumbers.js new file mode 100644 index 0000000..e5b9235 --- /dev/null +++ b/Chapter17-Moderate/swapNumbers.js @@ -0,0 +1,15 @@ +/* +Write a function to swap a number in place (that is, without any temporary variables). +*/ + +function switchVals(a,b) { + if (a > b) { + a = a - b; + b = a + b; + a = b - a; + } else if (b > a) { + b = b - a; + a = a + b; + b = a - b; + } +} \ No newline at end of file diff --git a/Chapter18-Hard/18.1-addition.js b/Chapter18-Hard/18.1-addition.js new file mode 100644 index 0000000..157b0cf --- /dev/null +++ b/Chapter18-Hard/18.1-addition.js @@ -0,0 +1,18 @@ +/* +Write a function that adds two numbers. You should not use + or any arithmetic operators. +*/ + +//convert numbers to binary in order to perform operations on them +function add(num1, num2) { + let bin1 = num1.toString(2); + let bin2 = num2.toString(2); + return parseInt(addBinary(bin1, bin2), 2); +} + +//recursively add binary numbers using XOR and AND +function addBinary(bin1, bin2) { + if (bin2 === 0) return bin1; + let sum = bin1 ^ bin2; + let carry = (bin1 & bin2) << 1; + return addBinary(sum, carry); +} \ No newline at end of file diff --git a/Chapter18-Hard/4.2-checkGraphRoute.js b/Chapter18-Hard/4.2-checkGraphRoute.js new file mode 100644 index 0000000..1be4be3 --- /dev/null +++ b/Chapter18-Hard/4.2-checkGraphRoute.js @@ -0,0 +1,45 @@ +/* +Problem: Given a directed graph, design an algorithm to find out whether there is a route between two nodes. +*/ + +var Graph = function(){ + this.nodes = []; +} + +Graph.prototype.addNode = function(node){ + this.nodes.push(node); +} + +var GraphNode = function(data) { + this.data = data; + this.edges = []; +} + +GraphNode.prototype.addEdge = function(node) { + this.edges.push(node); +} + +Graph.prototype.checkConnection = function(startNode, endNode) { + var connected = false; + var graph = this; + + //reduce potential runtime by storing edges we've already checked + var checkedEdges = []; + var checkEdge = function(edge) { + if (connected !== false) return; + if (edge.data === endNode.data) { + connected = true; + return; + } + else if (edge.edges.length === 0 || checkedEdges.indexOf(edge.data) > -1) return; + else { + checkedEdges.push(edge.data); + return edge.edges.forEach(function(subEdge){ return checkEdge(subEdge)}) + } + } + checkEdge(startNode, endNode) + + return connected; +} + +//runtime: O(n) \ No newline at end of file diff --git a/Chapter2-LinkedLists/2.5-AddLinkedLists.js b/Chapter2-LinkedLists/2.5-AddLinkedLists.js new file mode 100644 index 0000000..20ac2ed --- /dev/null +++ b/Chapter2-LinkedLists/2.5-AddLinkedLists.js @@ -0,0 +1,24 @@ +/* +Problem: You have two numbers represented by a linked list, where each node contains a sinle digit. THe digits are stored in reverse order, such that the 1's digits is at the head of the list. Write a function that adds the two numbers and returns the sum as a linked list. +*/ + +function addLinkedList(list1, list2) { + let newList = new LinkedList(); + let num1 = list1.head; + let num2 = list2.head; + var carry = 0; + while ((num1 && num1.data) || (num2 && num2.data)) { + console.log(num1.data) + if (!num1) num1 = {data: 0}; + if (!num2) num2 = {data: 0}; + let sum = num1.data + num2.data + carry; + newList.insert(sum % 10) + carry = sum >= 10 ? 1 : 0; + num1 = num1.next; + num2 =num2.next; + } + if (carry === 1) { + newList.insert(1); + } + return newList; +} \ No newline at end of file diff --git a/Chapter3-StacksandQueues/3.4-TowersOfHanoi.js b/Chapter3-StacksandQueues/3.4-TowersOfHanoi.js new file mode 100644 index 0000000..d2bf4c8 --- /dev/null +++ b/Chapter3-StacksandQueues/3.4-TowersOfHanoi.js @@ -0,0 +1,20 @@ +/* +Problem: In the classic problem of the Towers of Hanoi, you have 3 towers and N disks of different sixez which can slide onto any tower. The puzzle starts with. The puzzle starts wtih disks sorted in ascending order of size from top to bottom (each disk sits on top of an even larger one). You have the following constraints: +1. Only one disk can be moved at a time. +2. A disk is slid off the top of one tower onto the next rod. +3. A disk can onle be placed on top of a larger disk. + +Write a program to move the disks from the first to the last using Stacks. +*/ + +/** +@params: Three stacks, extends stack in data structure. +**/ +function moveDisks(origin, buffer, destination) { + while(origin.top) { + var top = origin.pop(); + moveDisks(origin, buffer, destination); + destination.push(top); + moveDisks(buffer, origin, destination); + } +} diff --git a/Chapter4-Trees&Graphs/4.2-checkGraphRoute.js b/Chapter4-Trees&Graphs/4.2-checkGraphRoute.js new file mode 100644 index 0000000..1be4be3 --- /dev/null +++ b/Chapter4-Trees&Graphs/4.2-checkGraphRoute.js @@ -0,0 +1,45 @@ +/* +Problem: Given a directed graph, design an algorithm to find out whether there is a route between two nodes. +*/ + +var Graph = function(){ + this.nodes = []; +} + +Graph.prototype.addNode = function(node){ + this.nodes.push(node); +} + +var GraphNode = function(data) { + this.data = data; + this.edges = []; +} + +GraphNode.prototype.addEdge = function(node) { + this.edges.push(node); +} + +Graph.prototype.checkConnection = function(startNode, endNode) { + var connected = false; + var graph = this; + + //reduce potential runtime by storing edges we've already checked + var checkedEdges = []; + var checkEdge = function(edge) { + if (connected !== false) return; + if (edge.data === endNode.data) { + connected = true; + return; + } + else if (edge.edges.length === 0 || checkedEdges.indexOf(edge.data) > -1) return; + else { + checkedEdges.push(edge.data); + return edge.edges.forEach(function(subEdge){ return checkEdge(subEdge)}) + } + } + checkEdge(startNode, endNode) + + return connected; +} + +//runtime: O(n) \ No newline at end of file diff --git a/Chapter4-Trees&Graphs/4.3-sortedArrayToBST.js b/Chapter4-Trees&Graphs/4.3-sortedArrayToBST.js new file mode 100644 index 0000000..3348e2d --- /dev/null +++ b/Chapter4-Trees&Graphs/4.3-sortedArrayToBST.js @@ -0,0 +1,17 @@ +/* +Problem: GIven a sorted (increasing oders) array with unique integer elements, write an algorithm to create a binary search tree with minimal height. +*/ + +//building off of the BST in Data Structures + +var createBST = function(arr){ + if (arr.length === 0) return null; + var midpoint = Math.floor(arr.length/2); + var tree = new BST(arr[midpoint]); + tree.left = createBST(arr.slice(0, midpoint)); + tree.right = createBST(arr.slice(midpoint + 1, arr.length)); + + return tree; +}; + +//runtime: O(n) \ No newline at end of file diff --git a/Chapter5-BitManipulation/5.2-decimalToBinary.js b/Chapter5-BitManipulation/5.2-decimalToBinary.js new file mode 100644 index 0000000..0031c8a --- /dev/null +++ b/Chapter5-BitManipulation/5.2-decimalToBinary.js @@ -0,0 +1,23 @@ +/** +@params(num) - decimal number +**/ + +var decimalToBinary = function(num) { + var string = '0.'; + var i = 1; + + while (num > 0) { + if (string.length > 32) return 'ERROR'; + if (num >= 1/Math.pow(2,i)) { + string += '1'; + num -= (1/Math.pow(2,i)); + } else { + string += '0'; + } + i++; + } + + return string; +}; + +//runtime: constant \ No newline at end of file