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 / countPaths.js
Created March 23, 2025 17:49
You are in a city that consists of n intersections numbered from 0 to n - 1 with bi-directional roads between some intersections. The inputs are generated such that you can reach any intersection from any other intersection and that there is at most
/**
* @param {number} n - Number of intersections (nodes)
* @param {number[][]} roads - 2D array representing roads where [a, b, time] means
* a road exists between a and b with travel time "time"
* @return {number} - Number of ways to reach intersection (n-1) in the shortest time
*/
var countPaths = function(n, roads) {
const MOD = 1_000_000_007; // Modular arithmetic base
// PART ONE: Build the graph and calculate shortest distances
@tatsuyax25
tatsuyax25 / countCompleteComponents.js
Last active March 22, 2025 17:34
You are given an integer n. There is an undirected graph with n vertices, numbered from 0 to n - 1. You are given a 2D integer array edges where edges[i] = [ai, bi] denotes that there exists an undirected edge connecting vertices ai and bi. Return t
/**
* @param {number} n
* @param {number[][]} edges
* @return {number}
*/
var countCompleteComponents = function(n, edges) {
// Initialize parent array (each vertex is its own parent initially)
let parent = Array.from({ length: n }, (_, i) => i);
// Rank array to optimize Union-Find operations
let rank = Array(n).fill(0);
@tatsuyax25
tatsuyax25 / findAllRecipes.js
Created March 21, 2025 19:02
You have information about n different recipes. You are given a string array recipes and a 2D string array ingredients. The ith recipe has the name recipes[i], and you can create it if you have all the needed ingredients from ingredients[i]. A recipe
/**
* @param {string[]} recipes
* @param {string[][]} ingredients
* @param {string[]} supplies
* @return {string[]}
*/
var findAllRecipes = function(recipes, ingredients, supplies) {
// Step 1: Initialization
const n = recipes.length; // Get the number of recipes
const graph = new Map(); // Represents dependencies: ingredients point to recipes they can help create
@tatsuyax25
tatsuyax25 / minimumCost.js
Created March 20, 2025 22:01
There is an undirected weighted graph with n vertices labeled from 0 to n - 1. You are given the integer n and an array edges, where edges[i] = [ui, vi, wi] indicates that there is an edge between vertices ui and vi with a weight of wi. A walk on a
/**
* Finds the minimum cost to connect queried nodes in a weighted graph.
*
* @param {number} n - The number of nodes in the graph.
* @param {number[][]} edges - The edges of the graph, where each edge is represented as [u, v, w].
* @param {number[][]} query - An array of queries, where each query is represented as [s, t].
* @return {number[]} - An array where each element corresponds to the minimum cost of connecting the queried nodes.
*/
var minimumCost = function(n, edges, query) {
// Initialize Union-Find (Disjoint Set Union) structures
@tatsuyax25
tatsuyax25 / minOperations.js
Created March 19, 2025 16:06
You are given a binary array nums. You can do the following operation on the array any number of times (possibly zero): Choose any 3 consecutive elements from the array and flip all of them. Flipping an element means changing its value from 0 to 1,
/**
* @param {number[]} nums
* @return {number}
*/
var minOperations = function(nums) {
let count = 0; // Initialize the counter for the number of flips
// Helper function to flip a single element at index i
function flip(i) {
nums[i] = nums[i] === 0 ? 1 : 0; // Toggle the element (0 -> 1, 1 -> 0)
@tatsuyax25
tatsuyax25 / longestNiceSubarray.js
Created March 18, 2025 19:46
You are given an array nums consisting of positive integers. We call a subarray of nums nice if the bitwise AND of every pair of elements that are in different positions in the subarray is equal to 0. Return the length of the longest nice subarray.
/**
* Finds the length of the longest "nice" subarray.
* A "nice" subarray is one where the bitwise AND of all elements is zero.
*
* @param {number[]} nums - Array of integers.
* @return {number} - Length of the longest nice subarray.
*/
var longestNiceSubarray = function(nums) {
let windowSizeMax = 1; // Tracks the maximum length of the nice subarray.
let left = 0; // Left pointer of the sliding window.
@tatsuyax25
tatsuyax25 / divideArray.js
Created March 17, 2025 20:09
You are given an integer array nums consisting of 2 * n integers. You need to divide nums into n pairs such that: Each element belongs to exactly one pair. The elements present in a pair are equal. Return true if nums can be divided into n pairs, o
/**
* @param {number[]} nums
* @return {boolean}
*/
var divideArray = function(nums) {
// Create a map to store the frequency of each number
let frequencyMap = new Map();
let length = nums.length;
// Populate the frequency map
@tatsuyax25
tatsuyax25 / repairCars .js
Created March 16, 2025 17:17
ou are given an integer array ranks representing the ranks of some mechanics. ranksi is the rank of the ith mechanic. A mechanic with a rank r can repair n cars in r * n2 minutes. You are also given an integer cars representing the total number of c
/**
* @param {number[]} ranks
* @param {number} cars
* @return {number}
*/
var repairCars = function (ranks, cars) {
// Initialize the binary search range:
// 'left' represents the minimum possible time (1 minute).
// 'right' is the maximum possible time (if the slowest mechanic repairs all cars).
let left = 1;
@tatsuyax25
tatsuyax25 / minCapability.js
Created March 15, 2025 16:09
There are several consecutive houses along a street, each of which has some money inside. There is also a robber, who wants to steal money from the homes, but he refuses to steal from adjacent homes. The capability of the robber is the maximum amoun
/**
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
var minCapability = function(nums, k) {
// Define the binary search range: minimum and maximum house values.
let left = Math.min(...nums), right = Math.max(...nums);
// Function to check if a given capability can rob at least `k` house.
@tatsuyax25
tatsuyax25 / maximumCandies.js
Created March 14, 2025 16:13
You are given a 0-indexed integer array candies. Each element in the array denotes a pile of candies of size candies[i]. You can divide each pile into any number of sub piles, but you cannot merge two piles together. You are also given an integer k.
/**
* @param {number[]} candies
* @param {number} k
* @return {number}
*/
var maximumCandies = function(candies, k) {
// Helper function to check if it's possible to distribute 'target' candies to 'k' children
function canDistribute(candies, k, target) {
let count = 0; // Count the number of children that can receive 'target' candies
for (let pile of candies) {