Skip to content

Instantly share code, notes, and snippets.

View tatsuyax25's full-sized avatar
:octocat:
Focusing

Miguel Urena tatsuyax25

:octocat:
Focusing
View GitHub Profile
@tatsuyax25
tatsuyax25 / findAllPeople.js
Created December 19, 2025 17:18
You are given an integer n indicating there are n people numbered from 0 to n - 1. You are also given a 0-indexed 2D integer array meetings where meetings[i] = [xi, yi, timei] indicates that person xi and person yi have a meeting at timei. A person m
/**
* @param {number} n
* @param {number[][]} meetings
* @param {number} firstPerson
* @return {number[]}
*/
// Function to find all people connected to the first person
var findAllPeople = function(n, meetings, firstPerson) {
// Initialize an array to keep track of connections
const connected = new Array(n).fill(-1);
@tatsuyax25
tatsuyax25 / maxProfit.js
Created December 18, 2025 18:10
You are given two integer arrays prices and strategy, where: prices[i] is the price of a given stock on the ith day. strategy[i] represents a trading action on the ith day, where: -1 indicates buying one unit of the stock. 0 indicates holding the st
/**
* Calculates the maximum profit achievable by applying a strategy
* and replacing a window of size `k` with a new profit calculation.
*
* @param {number[]} prices - Array of prices over time.
* @param {number[]} strategy - Strategy multipliers applied to each price.
* @param {number} k - Size of the window to adjust.
* @returns {number} Maximum profit achievable.
*/
var maxProfit = function(prices, strategy, k) {
@tatsuyax25
tatsuyax25 / maximumProfit.js
Created December 17, 2025 17:17
You are given an integer array prices where prices[i] is the price of a stock in dollars on the ith day, and an integer k. You are allowed to make at most k transactions, where each transaction can be either of the following: Normal transaction: Bu
/**
* @param {number[]} prices
* @param {number} k
* @return {number}
*/
var maximumProfit = function(prices, k) {
// Edge cases
const n = prices.length;
if (n === 0 || k === 0) return 0;
@tatsuyax25
tatsuyax25 / maxProfit.js
Created December 16, 2025 17:34
You are given an integer n, representing the number of employees in a company. Each employee is assigned a unique ID from 1 to n, and employee 1 is the CEO. You are given two 1-based integer arrays, present and future, each of length n, where: prese
/**
* @param {number} n
* @param {number[]} present
* @param {number[]} future
* @param {number[][]} hierarchy
* @param {number} budget
* @return {number}
*/
var maxProfit = function(n, present, future, hierarchy, budget) {
const employeeCount = n;
@tatsuyax25
tatsuyax25 / getDescentPeriods.js
Last active December 15, 2025 18:22
You are given an integer array prices representing the daily price history of a stock, where prices[i] is the stock price on the ith day. A smooth descent period of a stock consists of one or more contiguous days such that the price on each day is l
/**
* @param {number[]} prices
* @return {number}
*/
var getDescentPeriods = function(prices) {
let total = 0; // total number of descent periods
let streak = 0; // length of current descent streak
for (let i = 0; i < prices.length; i++) {
if (i > 0 && prices[i] === prices[i - 1] - 1) {
@tatsuyax25
tatsuyax25 / numberOfWays.js
Created December 14, 2025 18:44
Along a long library corridor, there is a line of seats and decorative plants. You are given a 0-indexed string corridor of length n consisting of letters 'S' and 'P' where each 'S' represents a seat and each 'P' represents a plant. One room divider
/**
* Calculates the number of ways to divide a corridor into valid sections.
* Each section must contain exactly two 'S' (seats).
* 'P' (plants) between pairs of seats determine possible partitions.
*
* @param {string} corridor - A string consisting of 'S' (seats) and 'P' (plants).
* @return {number} - The number of valid ways to divide the corridor.
*/
var numberOfWays = function(corridor) {
const MOD = 1e9 + 7; // Large prime modulus to prevent overflow
@tatsuyax25
tatsuyax25 / validateCoupons.js
Last active December 13, 2025 17:55
You are given three arrays of length n that describe the properties of n coupons: code, businessLine, and isActive. The ith coupon has: code[i]: a string representing the coupon identifier. businessLine[i]: a string denoting the business category of
/**
* @param {string[]} code
* @param {string[]} businessLine
* @param {boolean[]} isActive
* @return {string[]}
*/
var validateCoupons = function(code, businessLine, isActive) {
// Guard: ensure arrays exist and have equal length
if (!Array.isArray(code) || !Array.isArray(businessLine) || !Array.isArray(isActive)) return [];
const n = Math.min(code.length, businessLine.length, isActive.length);
@tatsuyax25
tatsuyax25 / countMentions.js
Created December 12, 2025 18:32
You are given an integer numberOfUsers representing the total number of users and an array events of size n x 3. Each events[i] can be either of the following two types: Message Event: ["MESSAGE", "timestampi", "mentions_stringi"] This event indica
/**
* @param {number} numberOfUsers
* @param {string[][]} events
* @return {number[]}
*/
var countMentions = function(numberOfUsers, events) {
// Result: mentions per user
const mentions = new Array(numberOfUsers).fill(0);
// Online state and offline-until times
@tatsuyax25
tatsuyax25 / countCoveredBuildings.js
Created December 11, 2025 19:04
You are given a positive integer n, representing an n x n city. You are also given a 2D grid buildings, where buildings[i] = [x, y] denotes a unique building located at coordinates [x, y]. A building is covered if there is at least one building in a
/**
* @param {number} n
* @param {number[][]} buildings
* @return {number}
*/
var countCoveredBuildings = function(n, buildings) {
// Guard: empty list -> no covered buildings
if (!buildings || buildings.length === 0) return 0;
// Step 1: Precompute extremes per row (y) and column (x).
@tatsuyax25
tatsuyax25 / countPermutations.js
Created December 10, 2025 21:35
You are given an array complexity of length n. There are n locked computers in a room with labels from 0 to n - 1, each with its own unique password. The password of the computer i has a complexity complexity[i]. The password for the computer label
/**
* @param {number[]} complexity
* @return {number}
*/
var countPermutations = function(complexity) {
const n = complexity.length;
const MOD = 1_000_000_007; // Large prime modulus for preventing overflow
// Check validity: all elements after the first must be strictly greater
for (let i = 1; i < n; i++) {