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 / countNegatives.js
Last active December 28, 2025 18:03
Given a m x n matrix grid which is sorted in non-increasing order both row-wise and column-wise, return the number of negative numbers in grid.
/**
* @param {number[][]} grid - A 2D matrix sorted in non-increasing order
* @return {number} - Total count of negative numbers in the matrix
*/
var countNegatives = function (grid) {
let count = 0;
// Loop through each row
for (let row = 0; row < grid.length; row++) {
@tatsuyax25
tatsuyax25 / bestClosingTime.js
Created December 26, 2025 18:19
You are given the customer visit log of a shop represented by a 0-indexed string customers consisting only of characters 'N' and 'Y': if the ith character is 'Y', it means that customers come at the ith hour whereas 'N' indicates that no customers c
/**
* Given a string of 'Y' (customer arrives) and 'N' (no customer),
* find the hour to close the shop that minimizes penalty.
*
* Penalty rules:
* - Staying open while no customers come → +1 penalty for each 'N'
* - Closing early while customers would have come → +1 penalty for each 'Y'
*
* @param {string} customers
* @return {number}
@tatsuyax25
tatsuyax25 / minimumBoxes.js
Created December 24, 2025 19:56
You are given an array apple of size n and an array capacity of size m. There are n packs where the ith pack contains apple[i] apples. There are m boxes as well, and the ith box has a capacity of capacity[i] apples. Return the minimum number of box
/**
* @param {number[]} apple
* @param {number[]} capacity
* @return {number}
*/
var minimumBoxes = function(apple, capacity) {
// 1. Compute the total number of apples across all packs.
// Since aplles can be split across boxes, only the total matters.
let totalApples = 0;
for (let a of apple) {
@tatsuyax25
tatsuyax25 / minDeletionSize.js
Created December 22, 2025 17:09
You are given an array of n strings strs, all of the same length. We may choose any deletion indices, and we delete all the characters in those indices for each string. For example, if we have strs = ["abcdef","uvwxyz"] and deletion indices {0, 2,
/**
* @param {string[]} strs
* @return {number}
*/
var minDeletionSize = function(strs) {
const n = strs.length; // number of rows
const m = strs[0].length; // number of columns (all strings same length)
// dp[i] = length of the longest valid chain ending at column i
const dp = Array(m).fill(1);
@tatsuyax25
tatsuyax25 / minDeletionSize.js
Created December 21, 2025 18:06
You are given an array of n strings strs, all of the same length. We may choose any deletion indices, and we delete all the characters in those indices for each string. For example, if we have strs = ["abcdef","uvwxyz"] and deletion indices {0, 2,
/**
* @param {string[]} strs
* @return {number}
*/
var minDeletionSize = function(strs) {
const n = strs.length;
const m = strs[0].length;
// sorted[i] = true means strs[i] < strs[i+1] is already determined
const sorted = Array(n - 1).fill(false);
@tatsuyax25
tatsuyax25 / minDeletionSize.js
Created December 20, 2025 21:30
You are given an array of n strings strs, all of the same length. The strings can be arranged such that there is one on each line, making a grid. For example, strs = ["abc", "bce", "cae"] can be arranged as follows: abc bce cae You want to delete t
/**
* @param {string[]} strs
* @return {number}
*/
var minDeletionSize = function(strs) {
// Number of rows (strings)
const n = strs.length;
// Number of columns (length of each string)
const m = strs[0].length;
@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;