Skip to content

Instantly share code, notes, and snippets.

View tatsuyax25's full-sized avatar
πŸ‘¨β€πŸ’»
Focusing

Miguel Urena tatsuyax25

πŸ‘¨β€πŸ’»
Focusing
View GitHub Profile
@tatsuyax25
tatsuyax25 / maxCandies.js
Created June 3, 2025 19:56
You have n boxes labeled from 0 to n - 1. You are given four arrays: status, candies, keys, and containedBoxes where: status[i] is 1 if the ith box is open and 0 if the ith box is closed, candies[i] is the number of candies in the ith box, keys[i] i
/**
* @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
@tatsuyax25
tatsuyax25 / candy.js
Created June 2, 2025 16:18
There are n children standing in a line. Each child is assigned a rating value given in the integer array ratings. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children w
/**
* @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);
@tatsuyax25
tatsuyax25 / distributeCandies.js
Created June 1, 2025 16:07
You are given two positive integers n and limit. Return the total number of ways to distribute n candies among 3 children such that no child gets more than limit candies.
/**
* 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.
@tatsuyax25
tatsuyax25 / closestMeetingNode.js
Created May 30, 2025 16:02
You are given a directed graph of n nodes numbered from 0 to n - 1, where each node has at most one outgoing edge. The graph is represented with a given 0-indexed array edges of size n, indicating that there is a directed edge from node i to node ed
/**
* 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
@tatsuyax25
tatsuyax25 / maxTargetNodes.js
Created May 29, 2025 16:46
There exist two undirected trees with n and m nodes, labeled from [0, n - 1] and [0, m - 1], respectively. You are given two 2D integer arrays edges1 and edges2 of lengths n - 1 and m - 1, respectively, where edges1[i] = [ai, bi] indicates that ther
/**
* 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());
@tatsuyax25
tatsuyax25 / maxTargetNodes.js
Created May 28, 2025 19:44
There exist two undirected trees with n and m nodes, with distinct labels in ranges [0, n - 1] and [0, m - 1], respectively. You are given two 2D integer arrays edges1 and edges2 of lengths n - 1 and m - 1, respectively, where edges1[i] = [ai, bi] i
/**
* 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) {
@tatsuyax25
tatsuyax25 / differenceOfSums.js
Created May 27, 2025 16:25
You are given positive integers n and m. Define two integers as follows: num1: The sum of all integers in the range [1, n] (both inclusive) that are not divisible by m. num2: The sum of all integers in the range [1, n] (both inclusive) that are div
/**
* @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
@tatsuyax25
tatsuyax25 / largestPathValue.js
Created May 26, 2025 15:59
There is a directed graph of n colored nodes and m edges. The nodes are numbered from 0 to n - 1. You are given a string colors where colors[i] is a lowercase English letter representing the color of the ith node in this graph (0-indexed). You are a
/**
* 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)
@tatsuyax25
tatsuyax25 / longestPalindrome.js
Last active May 25, 2025 16:52
You are given an array of strings words. Each element of words consists of two lowercase English letters. Create the longest possible palindrome by selecting some elements from words and concatenating them in any order. Each element can be selected
/**
* @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));
@tatsuyax25
tatsuyax25 / findWordsContaining.js
Created May 24, 2025 16:28
You are given a 0-indexed array of strings words and a character x. Return an array of indices representing the words that contain the character x. Note that the returned array may be in any order.
/**
* @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++) {