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 / 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++) {
@tatsuyax25
tatsuyax25 / maxRemoval.js
Created May 22, 2025 17:20
You are given an integer array nums of length n and a 2D array queries where queries[i] = [li, ri]. Each queries[i] represents the following action on nums: Decrement the value at each index in the range [li, ri] in nums by at most 1. The amount by
/**
* @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
@tatsuyax25
tatsuyax25 / setZeroes.js
Created May 21, 2025 16:54
Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0's. You must do it in place.
/**
* @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;
@tatsuyax25
tatsuyax25 / isZeroArray.js
Last active June 2, 2025 16:20
You are given an integer array nums of length n and a 2D array queries, where queries[i] = [li, ri]. For each queries[i]: Select a subset of indices within the range [li, ri] in nums. Decrement the values at the selected indices by 1. A Zero Array
/**
* @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