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
| export function findOrder(n, prerequisites) { | |
| const adjList = {}; | |
| // init adj list | |
| for (let i = 0; i < n; ++i) { | |
| adjList[i] = []; | |
| } | |
| // build adj list from prereqs | |
| for (let i = 0; i < prerequisites.length; ++i) { |
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
| export function canFinish(numCourses, prerequisites) { | |
| const adjList = {}; | |
| // initialize the adj list | |
| for (let i = 0; i < numCourses; ++i) { | |
| adjList[i] = []; | |
| } | |
| // build the adj list from prereq, where each prereq is a pair [to, from] | |
| // where from is a course that must be taken before course to |
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
| export function findRecipes(recipes, ingredients, supplies) { | |
| // count dependencies of each recipe | |
| const deps = {}; | |
| const adjList = []; | |
| // initialize adj list and deps map from recipes and supplies | |
| recipes.forEach(recipe => { | |
| adjList[recipe] = []; | |
| deps[recipe] = 0; | |
| }); |
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 setMatrixZeros(mat) { | |
| let frow = false; | |
| let fcol = false; | |
| const rows = mat.length; | |
| const cols = mat[0].length; | |
| // if any element in the first row or in the first column is 0, set frow / fcol to true | |
| for (let col = 0; col < cols; ++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
| function rotateImage(matrix) { | |
| let l = 0; | |
| let r = matrix.length - 1; | |
| while (l < r) { | |
| for (let i = 0; i < (r - l); ++i) { | |
| const t = l; | |
| const b = r; | |
| const topLeft = matrix[t][l + i]; | |
| matrix[t][l + i] = matrix[b - i][l]; | |
| matrix[b - i][l] = matrix[b][r - i]; |
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 spiralOrder(matrix) { | |
| const result = []; | |
| // initialize direction variable (1 or -1) | |
| let dir = 1; | |
| // current row | |
| let row = 0; | |
| // current column (we have to start "outside" since we don't want to go out of bounds) | |
| // current row starts at 0 and not -1 because our first step is to move right, and it's changing columns | |
| // after moving right our first row is complete, so row=0 is actually "outside", just as col=-1 is. |
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
| export function findExitColumn(matrix) { | |
| // number of balls = number of columns = length of the first row since all rows are equal length | |
| const balls = matrix[0].length; | |
| // initialize result array, size = number of balls, initial values = initial columns of balls (0 for ball 0, n for ball n) | |
| const result = Array(balls).fill(null).map((_, i) => i); | |
| // iterate matrix row, from 0 to last row | |
| for (let row = 0; row < matrix.length; ++row) { | |
| // at each row, iterate ball positions |
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
| export function calculator(expression) { | |
| // so we have a stack, and an expression which is a string | |
| // we iterate one character from the expression at a time | |
| // so we need a variable (current number) and another (result) and another (sign) | |
| // and a stack | |
| const stack = []; | |
| let currentNumber = ""; | |
| let sign = 1; |
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
| export function removeDuplicates(string) { | |
| const stack = []; | |
| for (const char of string) { | |
| if (stack[stack.length - 1] === char) { | |
| stack.pop(); | |
| } else { | |
| stack.push(char); | |
| } | |
| } |
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
| export function minRemoveParentheses(s) { | |
| const split = s.split(""); | |
| const stack = []; | |
| for (let i = 0; i < split.length; ++i) { | |
| const char = split[i]; | |
| if ( | |
| char === ")" && | |
| stack.length > 0 && | |
| stack[stack.length - 1][0] === "(" |