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 / 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) {
@tatsuyax25
tatsuyax25 / maxFrequency.js
Created October 22, 2025 16:37
You are given an integer array nums and two integers k and numOperations. You must perform an operation numOperations times on nums, where in each operation you: Select an index i that was not selected in any previous operations. Add an integer in
/**
* @param {number[]} nums
* @param {number} k
* @param {number} numOperations
* @return {number}
*/
var maxFrequency = function (nums, k, numOperations) {
// Sort the array to enable binary search and range grouping
nums.sort((a, b) => a - b);
@tatsuyax25
tatsuyax25 / maxFrequency.js
Created October 21, 2025 18:38
You are given an integer array nums and two integers k and numOperations. You must perform an operation numOperations times on nums, where in each operation you: Select an index i that was not selected in any previous operations. Add an integer in
/**
* @param {number[]} nums - The input array of integers
* @param {number} k - The maximum value that can be added/subtracted in one operation
* @param {number} numOperations - The total number of operations allowed
* @return {number} - The maximum frequency of any element after operations
*/
var maxFrequency = function(nums, k, numOperations) {
const n = nums.length;
// Sort the array to enable binary search and range grouping
@tatsuyax25
tatsuyax25 / finalValueAfterOperations.js
Created October 20, 2025 15:22
There is a programming language with only four operations and one variable X: ++X and X++ increments the value of the variable X by 1. --X and X-- decrements the value of the variable X by 1. Initially, the value of X is 0. Given an array of string
/**
* @param {string[]} operations
* @return {number}
*/
var finalValueAfterOperations = function(operations) {
// Step 1: Initialize the variable X to 0
let X = 0;
// Step 2: Loop through each operation in the array
for (let i = 0; i < operations.length; i++) {
@tatsuyax25
tatsuyax25 / findLexSmallestString.js
Created October 19, 2025 17:01
You are given a string s of even length consisting of digits from 0 to 9, and two integers a and b. You can apply either of the following two operations any number of times and in any order on s: Add a to all odd indices of s (0-indexed). Digits po
/**
* @param {string} s
* @param {number} a
* @param {number} b
* @return {string}
*/
var findLexSmallestString = function(s, a, b) {
// Set to keep track of visited strings to avoid revisiting the same state
const visited = new Set();
@tatsuyax25
tatsuyax25 / maxPartitionsAfterOperations.js
Created October 17, 2025 16:07
You are given a string s and an integer k. First, you are allowed to change at most one index in s to another lowercase English letter. After that, do the following partitioning operation until s is empty: Choose the longest prefix of s containing
/**
* Calculates the maximum number of partitions after performing operations
* that allow up to `k` distinct characters per partition.
*
* @param {string} s - Input string consisting of lowercase letters.
* @param {number} k - Maximum number of distinct characters allowed per partition.
* @return {number} - Maximum number of partitions achievable.
*/
var maxPartitionsAfterOperations = function(s, k) {
const n = s.length;