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 / minOperations.js
Created November 12, 2025 17:23
You are given a 0-indexed array nums consisiting of positive integers. You can do the following operation on the array any number of times: Select an index i such that 0 <= i < n - 1 and replace either of nums[i] or nums[i+1] with their gcd value. R
/**
* @param {number[]} nums
* @return {number}
*/
var minOperations = function(nums) {
// Step 1: Quick checks for trivial cases
// If the array already contains a 1, spreading it is possible.
// If the gcd of the entire array > 1, it's impossible to ever reach 1.
const gcd = (a, b) => {
@tatsuyax25
tatsuyax25 / findMaxForm.js
Created November 11, 2025 17:10
You are given an array of binary strings strs and two integers m and n. Return the size of the largest subset of strs such that there are at most m 0's and n 1's in the subset. A set x is a subset of a set y if all elements of x are also elements o
/**
* @param {string[]} strs
* @param {number} m
* @param {number} n
* @return {number}
*/
var findMaxForm = function(strs, m, n) {
// Step 1: Initialize a 2D DP array
// dp[i][j] = max number of strings we can form with i zeros and j ones
let dp = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));
@tatsuyax25
tatsuyax25 / minOperations.js
Created November 10, 2025 17:14
You are given an array nums of size n, consisting of non-negative integers. Your task is to apply some (possibly zero) operations on the array so that all elements become 0. In one operation, you can select a subarray [i, j] (where 0 <= i <= j < n)
/**
* Calculates the minimum number of operations needed to build a strictly increasing stack
* from the input array, ignoring zeros and removing elements that break the order.
*
* @param {number[]} nums - Array of integers to process.
* @return {number} - Minimum number of operations performed.
*/
var minOperations = function(nums) {
const stack = []; // Stack to maintain increasing sequence
let operations = 0; // Counter for operations performed
@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