This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var solveSudoku = function(board, row=0, col=0) { | |
| function isSafe(row, col, num) { | |
| return isRowSafe(row, num) && isColSafe(col, num) && isGridSafe(row, col, num); | |
| } | |
| function isRowSafe(row, num) { | |
| for (let col=0; col<board.length;col++) { | |
| if (board[row][col] === num) return false; | |
| } | |
| return true; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function findBestStartingSpot(matrix) { | |
| let bestStartingSpot = []; | |
| let min = Infinity; | |
| function validMove(row, col, matrixDup) { | |
| return row >= 0 && | |
| row < matrixDup.length && | |
| col >= 0 && | |
| col < matrixDup[0].length && | |
| matrixDup[row][col] !== '*'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Hotel { | |
| constructor(numRooms) { | |
| this.totalRooms = numRooms; | |
| this.reservations = {}; | |
| } | |
| book(startDay, endDay) { | |
| let checkIn = this._keyify(startDay); | |
| // do we have any reservations at this date | |
| if (this.reservations[checkIn] && this.reservations[checkIn].length) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function stringify(input) { | |
| if (!input) return "null"; | |
| if (typeof(input) === 'string') return input; | |
| if (typeof(input) === 'number') return `"${input}"`; | |
| if (Array.isArray(input)) { | |
| let result = []; | |
| input.forEach(value => { | |
| result.push(stringify(value)); | |
| }); | |
| return `[${result}]`; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class GraphNode { | |
| constructor(label) { | |
| this.label = label; | |
| this.neighbors = new Set(); | |
| this.color = null; | |
| } | |
| } | |
| const nodeA = new GraphNode('A'); | |
| const nodeB = new GraphNode('B'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function binarySearch(sortedArray, searchValue, minIndex=0, maxIndex=sortedArray.length-1) { | |
| let currentElement, middleIndex; | |
| while (minIndex <= maxIndex) { | |
| // Find the value of the middle of the array | |
| middleIndex = Math.floor((minIndex + maxIndex) / 2); | |
| currentElement = sortedArray[middleIndex]; | |
| // It's the same as the value in the middle - we can return! | |
| if (currentElement === searchValue) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| postOrder(result=[]) { | |
| if (!this.left && !this.right) { | |
| return result.push(this.key); | |
| } | |
| if (this.left) { | |
| this.left.postOrder(result); | |
| } | |
| if (this.right) { | |
| this.right.postOrder(result); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| inOrder(result=[]) { | |
| if (!this.left && !this.right) { | |
| return result.push(this.key); | |
| } | |
| if (this.left) { | |
| this.left.inOrder(result); | |
| } | |
| result.push(this.key); | |
| if (this.right) { | |
| this.right.inOrder(result); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| preOrder(result=[]) { | |
| if (!this.left && !this.right) { | |
| return result.push(this.key); | |
| } | |
| result.push(this.key); | |
| if (this.left) { | |
| this.left.preOrder(result); | |
| } | |
| if (this.right) { | |
| this.right.preOrder(result); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| bfs(result=[]) { | |
| const queue = new Queue(); | |
| queue.enqueue(this); | |
| let node; | |
| while (queue.length) { | |
| node = queue.dequeue(); | |
| result.push(node.key); | |
| if (node.left) { |
NewerOlder