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 exclusiveTime(n, events) { | |
| const result = Array(n).fill(0); | |
| const stack = []; | |
| for (const event of events) { | |
| const [id, action, time] = event.split(":"); | |
| if (action === "start") { | |
| stack.push([id, action, time]); | |
| } | |
| if (action === "end") { |
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 isValid(s) { | |
| const stack = []; | |
| const open = ["(", "[", "{"]; | |
| const closing = [")", "]", "}"]; | |
| for (const char of s) { | |
| const index = closing.indexOf(char); | |
| if (index !== -1 && stack[stack.length - 1] === open[index]) { | |
| stack.pop(); |
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 networkDelayTime(times, n, k) { | |
| // times[i] = (xi, yi, ti) | |
| // k - starting node | |
| // n = total nodes | |
| // so we need an adj list | |
| // a priority queue | |
| // a visited set | |
| // and a result variable | |
| // build an adjacency list from the times array |
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 numberOfPaths(n, corridors) { | |
| // n rooms | |
| // corridors[i]=[room1,room2] meaning corridor between room1 and room2 | |
| // find confusion score of the maze | |
| // confusion score = how many different cycles of length | |
| // 1-2-3-1 is a cycle of length 3 | |
| // if one or more rooms is different in another cycle, it's a different cycle | |
| let cycles = 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 validTree(n, edges) { | |
| // nodes from 0 to n-1 | |
| // edges[i] = [x,y] edge between node x and node y | |
| // determine if graph is a valid tree (no cycles, no detached nodes) | |
| // this condition catches all cases when there are cycles (additional edges) | |
| if (edges.length !== n - 1) { | |
| return false; | |
| } |
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 minimumBuses(matrix, s, d) { | |
| // at first we create an adj list, | |
| // where keys are stops | |
| // and values are array of buses visiting that stop | |
| // then we have a queue to run bfs | |
| // since bfs always finds shortest path in an unweighted graph | |
| // we add a tuple [stop, buses] to the queue | |
| // where buses is the number of different buses we took to reach the stop | |
| // initially buses is 0 since it's a starting stop |
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
| // Definition of a binary tree node | |
| // class TreeNode { | |
| // constructor(data) { | |
| // this.data = data; | |
| // this.left = null; | |
| // this.right = null; | |
| // } | |
| // } | |
| export function flattenTree(root) { |
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
| import { TreeNode } from "./ds_v1/BinaryTree.js"; | |
| // Definition of a binary tree node | |
| // class TreeNode { | |
| // constructor(data) { | |
| // this.data = data; | |
| // this.left = null; | |
| // this.right = null; | |
| // } | |
| // } |
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
| // Definition of a binary tree node | |
| // class TreeNode { | |
| // constructor(data) { | |
| // this.data = data; | |
| // this.left = null; | |
| // this.right = null; | |
| // } | |
| // } | |
| import { TreeNode } from "./ds_v1/BinaryTree.js"; |
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
| // Definition of a binary tree node | |
| // class TreeNode { | |
| // constructor(data) { | |
| // this.data = data; | |
| // this.left = null; | |
| // this.right = null; | |
| // } | |
| // } | |
| import { TreeNode } from "./ds_v1/BinaryTree.js"; |