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 / minCost.js
Created November 3, 2025 17:24
Alice has n balloons arranged on a rope. You are given a 0-indexed string colors where colors[i] is the color of the ith balloon. Alice wants the rope to be colorful. She does not want two consecutive balloons to be of the same color, so she asks Bo
/**
* Removes balloons to ensure no two adjacent balloons have the same color,
* minimizing the total removal time.
*
* @param {string} colors - A string where each character represents a balloon's color.
* @param {number[]} neededTime - An array where each element is the time to remove the corresponding balloon.
* @return {number} - The minimum total time to remove balloons to satisfy the condition.
*/
var minCost = function(colors, neededTime) {
let totalTime = 0; // Total time to remove balloons
@tatsuyax25
tatsuyax25 / getSneakyNumbers.js
Created October 31, 2025 15:41
In the town of Digitville, there was a list of numbers called nums containing integers from 0 to n - 1. Each number was supposed to appear exactly once in the list, however, two mischievous numbers sneaked in an additional time, making the list longe
/**
* Finds the two "sneaky" numbers added to an array that originally contained all numbers from 0 to n-1.
* The final array has length n + 2, and includes two unknown extra numbers.
*
* @param {number[]} nums - Array containing numbers from 0 to n-1 plus two extra unknowns.
* @return {number[]} - The two extra numbers that were added.
*/
var getSneakyNumbers = function (nums) {
const n = nums.length - 2; // Original range was 0 to n-1, so n = nums.length - 2
let xorAll = 0;
@tatsuyax25
tatsuyax25 / minNumberOperations.js
Last active October 30, 2025 16:53
You are given an integer array target. You have an integer array initial of the same size as target with all elements initially zeros. In one operation you can choose any subarray from initial and increment each value by one. Return the minimum num
/**
* @param {number[]} target
* @return {number}
*/
var minNumberOperations = function(target) {
// Initialize the total number of operations needed
let operations = 0;
// Loop through the target array
for (let i = 0; i < target.length; i++) {
@tatsuyax25
tatsuyax25 / smallestNumber.js
Created October 29, 2025 01:26
You are given a positive number n. Return the smallest number x greater than or equal to n, such that the binary representation of x contains only set bits
/**
* @param {number} n
* @return {number}
*/
var smallestNumber = function(n) {
// Start with 1 and keep shifting left and OR-ing to build numbers like 1, 3, 7, 15, 31, etc.
let x = 1;
// Keep generating numbers with all bits set until we find one >= n
while (x < n) {
@tatsuyax25
tatsuyax25 / countValidSelections.js
Created October 28, 2025 19:36
You are given an integer array nums. Start by selecting a starting position curr such that nums[curr] == 0, and choose a movement direction of either left or right. After that, you repeat the following process: If curr is out of the range [0, n -
/**
* Counts how many zero positions in the array can lead to all elements being reduced to zero
* by moving forward or backward and decrementing non-zero values.
*
* @param {number[]} nums - Array of non-negative integers
* @return {number} - Total number of valid selections from zero positions
*/
var countValidSelections = function (nums) {
// Collect indices where the value is zero
const zeroIndexArr = nums.reduce((acc, cur, idx) => {
@tatsuyax25
tatsuyax25 / numberOfBeams.js
Last active October 27, 2025 17:11
Anti-theft security devices are activated inside a bank. You are given a 0-indexed binary string array bank representing the floor plan of the bank, which is an m x n 2D matrix. bank[i] represents the ith row, consisting of '0's and '1's. '0' means t
/**
* @param {string[]} bank
* @return {number}
*/
// Function to calculate the total number of laser beams between security devices
var numberOfBeams = function(bank) {
// Array to store the count of devices (i.e., '1's) in each non-empty row
let arr = [];
// Variable to accumulate the total number of beams
@tatsuyax25
tatsuyax25 / Bank.js
Created October 26, 2025 17:47
You have been tasked with writing a program for a popular bank that will automate all its incoming transactions (transfer, deposit, and withdraw). The bank has n accounts numbered from 1 to n. The initial balance of each account is stored in a 0-inde
/**
* Bank constructor initializes the bank with account balances.
* @param {number[]} balance - Array of initial balances for each account (1-indexed).
*/
var Bank = function(balance) {
// Create an array of accounts with an extra slot at index 0 (unused).
// This allows 1-based indexing for accounts.
this.accounts = Array.from({ length: balance.length + 1 }, (_, i) => balance[i - 1] ?? 0);
// Store the total number of accounts including the unused 0th index.
@tatsuyax25
tatsuyax25 / totalMoney.js
Created October 25, 2025 17:39
Hercy wants to save money for his first car. He puts money in the Leetcode bank every day. He starts by putting in $1 on Monday, the first day. Every day from Tuesday to Sunday, he will put in $1 more than the day before. On every subsequent Monday,
/**
* @param {number} n
* @return {number}
*/
// Function to claculate the total amount of money saved over 'n' days
var totalMoney = function(n) {
let total = 0; // Initialize total money saved
let weekDay = 1; // Start from the first day of the week
let weekNumber = 1; // Start from the first week
@tatsuyax25
tatsuyax25 / nextBeautifulNumber.js
Last active October 24, 2025 16:15
An integer x is numerically balanced if for every digit d in the number x, there are exactly d occurrences of that digit in x. Given an integer n, return the smallest numerically balanced number strictly greater than n.
/**
* Finds the next "beautiful" number greater than the given input.
* A beautiful number is defined as one where each digit appears exactly as many times as its value.
* For example, 22 is beautiful because digit 2 appears twice.
*
* @param {number} n - The input number.
* @return {number} - The next beautiful number greater than n.
*/
var nextBeautifulNumber = function(n) {
// Precomputed list of beautiful numbers in ascending order.
@tatsuyax25
tatsuyax25 / hasSameDigits.js
Created October 23, 2025 16:28
You are given a string s consisting of digits. Perform the following operation repeatedly until the string has exactly two digits: For each pair of consecutive digits in s, starting from the first digit, calculate a new digit as the sum of the two d
/**
* @param {string} s
* @return {boolean}
*/
var hasSameDigits = function(s) {
// Convert the input string into an array of digits (as numbers)
let digits = s.split('').map(Number);
// Repeat the transformation until only two digits remain
while (digits.length > 2) {