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 / checkValidCuts.js
Created March 25, 2025 20:32
You are given an integer n representing the dimensions of an n x n grid, with the origin at the bottom-left corner of the grid. You are also given a 2D array of coordinates rectangles, where rectangles[i] is in the form [startx, starty, endx, endy],
/**
* Function to check if two horizontal or two vertical cuts can divide the grid
* into three sections, with each section containing at least one rectangle.
* @param {number} n - The dimensions of the n x n grid (not directly used here).
* @param {number[][]} rectangles - Array of rectangles where each rectangle is defined as [startx, starty, endx, endy].
* @return {boolean} - Returns true if valid cuts can be made; otherwise, false.
*/
var checkValidCuts = function(n, rectangles) {
// Arrays to store x and y intervals for each rectangle
let x = [];
@tatsuyax25
tatsuyax25 / countDays.js
Created March 24, 2025 19:05
You are given a positive integer days representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array meetings of size n where, meetings[i] = [start_i, end_i] represents the starting and en
/**
* Function to calculate the number of days an employee is free from meetings.
*
* @param {number} days - Total number of days the employee is available for work.
* @param {number[][]} meetings - Array of meeting intervals where each meeting is [start, end].
* @returns {number} - The count of days when the employee is free from meetings.
*/
var countDays = function(days, meetings) {
// Sort meetings by their start day to process them in chronological order
meetings.sort((a, b) => a[0] - b[0]);
@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;