π¨βπ»
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[]} status - Array indicating whether each box is open (1) or closed (0). | |
* @param {number[]} candies - Number of candies in each box. | |
* @param {number[][]} keys - List of keys contained in each box. | |
* @param {number[][]} containedBoxes - List of boxes contained in each box. | |
* @param {number[]} initialBoxes - Labels of the boxes you start with. | |
* @return {number} - Maximum number of candies you can collect. | |
*/ | |
var maxCandies = function(status, candies, keys, containedBoxes, initialBoxes) { | |
const queue = [...initialBoxes]; // Queue to process available boxes |
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[]} ratings | |
* @return {number} | |
*/ | |
var candy = function(ratings) { | |
const length = ratings.length; | |
// Initialize candies array, giving each child at least one candy. | |
const candies = new Array(length).fill(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
/** | |
* Calculates the number of ways to distribute `n` candies among 3 children | |
* ensuring no child gets more than `limit` candies. | |
* | |
* @param {number} n - Total number of candies. | |
* @param {number} limit - Maximum candies each child can receive. | |
* @return {number} - The number of valid distributions. | |
*/ | |
var distributeCandies = function(n, limit) { | |
let count = 0; // Stores the total number of valid distributions. |
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++) { |