π¨βπ»
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
/** | |
* Finds the closest meeting node between two starting nodes in a directed graph. | |
* | |
* @param {number[]} edges - Array representing directed edges of the graph. | |
* @param {number} node1 - First starting node. | |
* @param {number} node2 - Second starting node. | |
* @return {number} - The closest meeting node or -1 if no valid meeting node exists. | |
*/ | |
// Function to initialize an adjacency list representation of the graph |
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
/** | |
* Computes the maximum number of target nodes achievable. | |
* | |
* @param {number[][]} edges1 - List of edges in the first graph. | |
* @param {number[][]} edges2 - List of edges in the second graph. | |
* @return {number[]} - Maximum number of target nodes for each node in graph1. | |
*/ | |
var maxTargetNodes = function (edges1, edges2) { | |
// Determine the highest numbered node in each graph | |
let m = Math.max(...edges1.flat()); |
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
/** | |
* Computes the maximum number of target nodes in Tree 1 after optimally connecting | |
* a node in Tree 1 to a node in Tree 2. | |
* | |
* @param {number[][]} edges1 - List of edges representing the first tree. | |
* @param {number[][]} edges2 - List of edges representing the second tree. | |
* @param {number} k - Maximum allowed distance for a node to be considered targetable. | |
* @return {number[]} - Array where answer[i] represents the maximum reachable nodes for node i in Tree 1. | |
*/ | |
var maxTargetNodes = function (edges1, edges2, k) { |
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
/** | |
* @param {number} n | |
* @param {number} m | |
* @return {number} | |
*/ | |
var differenceOfSums = function(n, m) { | |
let num1 = 0; // Sum of numbers NOT divisible by m | |
let num2 = 0; // Sum of numbers divisible by m | |
// Loop through all numbers from 1 to n |
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
/** | |
* This function calculates the largest path value in a directed graph | |
* where each node is colored with a lowercase English letter. | |
* | |
* @param {string} colors - A string representing the colors of nodes (each character is a node color). | |
* @param {number[][]} edges - An array of directed edges between nodes. | |
* @return {number} - The largest path value, or -1 if the graph contains a cycle. | |
*/ | |
var largestPathValue = function(colors, edges) { | |
const n = colors.length, m = 26; // n = number of nodes, m = 26 (total lowercase English letters) |
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
/** | |
* @param {string[]} words | |
* @return {number} | |
*/ | |
var longestPalindrome = function(words) { | |
let result = 0; | |
// 2D array to keep track of word pairs, initialized to 0 | |
let cnt = Array(26).fill().map(() => Array(26).fill(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
/** | |
* @param {string[]} words | |
* @param {character} x | |
* @return {number[]} | |
*/ | |
var findWordsContaining = function(words, x) { | |
const indices = []; // Array to store indices of words containing 'x' | |
// Iterate through each word in the 'words' array | |
for (let i = 0; i < words.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
/** | |
* @param {number[]} nums | |
* @param {number[][]} queries | |
* @return {number} | |
*/ | |
var maxRemoval = function(nums, queries) { | |
const n = nums.length; | |
const m = queries.length; | |
// Step 1: Sort queries by start index to process efficiently |
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
/** | |
* @param {number[][]} matrix | |
* @return {void} Do not return anything, modify matrix in-place instead. | |
*/ | |
var setZeroes = function(matrix) { | |
const rows = matrix.length; | |
const cols = matrix[0].length; | |
let firstRowHasZero = false; | |
let firstColHasZero = 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
/** | |
* @param {number[]} nums | |
* @param {number[][]} queries | |
* @return {boolean} | |
*/ | |
var isZeroArray = function(nums, queries) { | |
// Create a difference array initialized with zeros | |
let diff = new Array(nums.length + 1).fill(0); | |
// Apply range updates using the difference array |