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 / countOperations.js
Last active November 9, 2025 17:26
You are given two non-negative integers num1 and num2. In one operation, if num1 >= num2, you must subtract num2 from num1, otherwise subtract num1 from num2. For example, if num1 = 5 and num2 = 4, subtract num2 from num1, thus obtaining num1 = 1 a
/**
* @param {number} num1
* @param {number} num2
* @return {number}
*/
var countOperations = function(num1, num2) {
// initialize a counter to keep track of the number of operations
let operations = 0;
// Continue looping until either num1 or num2 becomes 0
@tatsuyax25
tatsuyax25 / minimumOneBitOperations.js
Created November 8, 2025 18:46
Given an integer n, you must transform it into 0 using the following operations any number of times: Change the rightmost (0th) bit in the binary representation of n. Change the ith bit in the binary representation of n if the (i-1)th bit is set to
/**
* Calculates the minimum number of operations to reduce a number `n` to 0
* using a specific bitwise operation rule:
* - You can flip the lowest 1-bit and all bits to its right.
*
* This function uses a recursive approach based on the properties of Gray code.
*
* @param {number} n - The input number.
* @return {number} - Minimum number of operations to reduce `n` to 0.
*/
@tatsuyax25
tatsuyax25 / maxPower.js
Last active November 7, 2025 15:53
You are given a 0-indexed integer array stations of length n, where stations[i] represents the number of power stations in the ith city. Each power station can provide power to every city in a fixed range. In other words, if the range is denoted by
/**
* @param {number[]} stations - Initial number of power stations in each city
* @param {number} r - Range of each power station (affects cities within |i - j| <= r)
* @param {number} k - Number of additional power stations that can be built
* @return {number} - Maximum possible minimum power across all cities
*/
var maxPower = function (stations, r, k) {
const n = stations.length;
// Step 1: Build prefix sum array to quickly compute power in a range
@tatsuyax25
tatsuyax25 / processQueries.js
Last active November 6, 2025 17:41
You are given an integer c representing c power stations, each with a unique identifier id from 1 to c (1‑based indexing). These stations are interconnected via n bidirectional cables, represented by a 2D array connections, where each element connec
/**
* Simulates maintenance and shutdown queries on a network of power stations.
*
* @param {number} c - Number of power stations (1-indexed).
* @param {number[][]} connections - Bidirectional cables between stations.
* @param {number[][]} queries - List of queries: [1, x] for maintenance, [2, x] to shut down.
* @return {number[]} - Results of maintenance queries.
*/
var processQueries = function(c, connections, queries) {
// ----- Union-Find (Disjoint Set Union) Setup -----
@tatsuyax25
tatsuyax25 / findXSum.js
Created November 5, 2025 20:38
You are given an array nums of n integers and two integers k and x. The x-sum of an array is calculated by the following procedure: Count the occurrences of all elements in the array. Keep only the occurrences of the top x most frequent elements. I
/**
* @param {number[]} nums - Input array of numbers
* @param {number} k - Size of the sliding window
* @param {number} x - Number of top elements to sum (based on freq × value)
* @return {number[]} - Array of sums for each window
*/
var findXSum = function(nums, k, x) {
const n = nums.length;
// Shortcut: if k === x, just sum the window directly
@tatsuyax25
tatsuyax25 / findXSum.js
Last active November 4, 2025 18:39
You are given an array nums of n integers and two integers k and x. The x-sum of an array is calculated by the following procedure: Count the occurrences of all elements in the array. Keep only the occurrences of the top x most frequent elements. I
/**
* Computes a sliding window sum based on the x most frequent elements.
*
* @param {number[]} nums - The input array of numbers.
* @param {number} k - The size of the sliding window.
* @param {number} x - The number of top frequent elements to include in the sum.
* @return {number[]} - An array of sums for each window.
*/
var findXSum = function(nums, k, x) {
const result = [];
@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) {