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 / minimumOperations.js
Last active November 22, 2025 17:09
You are given an integer array nums. In one operation, you can add or subtract 1 from any element of nums. Return the minimum number of operations to make all elements of nums divisible by 3.
/**
* @param {number[]} nums
* @return {number}
*/
var minimumOperations = function(nums) {
// Initialize a counter to keep track of total operations
let operations = 0;
// Loop through each number in the array
for (let num of nums) {
@tatsuyax25
tatsuyax25 / intersectionSizeTwo.js
Created November 20, 2025 21:06
You are given a 2D integer array intervals where intervals[i] = [starti, endi] represents all the integers from starti to endi inclusively. A containing set is an array nums where each interval from intervals has at least two integers in nums. For
/**
* Finds the minimum number of integers needed so that
* each interval contains at least two chosen numbers.
*
* @param {number[][]} intervals - Array of [start, end] intervals
* @return {number} - Minimum number of integers chosen
*/
var intersectionSizeTwo = function(intervals) {
// Step 1: Sort intervals
// - Primary: by end ascending (smaller right boundary first)
@tatsuyax25
tatsuyax25 / findFinalValue.js
Created November 19, 2025 17:27
You are given an array of integers nums. You are also given an integer original which is the first number that needs to be searched for in nums. You then do the following steps: If original is found in nums, multiply it by two (i.e., set original =
/**
* @param {number[]} nums
* @param {number} original
* @return {number}
*/
var findFinalValue = function(nums, original) {
// Step 1: Convert nums into a Set for faster lookups.
// Why? Searching in an array is O(n), but in a Set it's O(1).
let numSet = new Set(nums);
@tatsuyax25
tatsuyax25 / isOneBitCharacter.js
Created November 18, 2025 21:57
We have two special characters: The first character can be represented by one bit 0. The second character can be represented by two bits (10 or 11). Given a binary array bits that ends with 0, return true if the last character must be a one-bit char
/**
* @param {number[]} bits
* @return {boolean}
*/
var isOneBitCharacter = function(bits) {
// Start from the first bit
let i = 0;
// Traverse until the second-to-last bit
// (because the last bit is always 0, we want to see if it's consumed or not)
@tatsuyax25
tatsuyax25 / kLengthApart.js
Created November 17, 2025 16:42
Given an binary array nums and an integer k, return true if all 1's are at least k places away from each other, otherwise return false.
/**
* @param {number[]} nums
* @param {number} k
* @return {boolean}
*/
var kLengthApart = function(nums, k) {
// Keep track of the index of the last '1' we saw
let lastOneIndex = -1;
// Loop through the array
@tatsuyax25
tatsuyax25 / numSub.js
Created November 16, 2025 17:47
Given a binary string s, return the number of substrings with all characters 1's. Since the answer may be too large, return it modulo 109 + 7.
/**
* @param {string} s
* @return {number}
*/
var numSub = function(s) {
const MOD = 1_000_000_007; // Large prime for modulo operations
let ans = 0; // Final answer accumulator
let run = 0; // Current streak length of consecutive '1's
for (let i = 0; i < s.length; i++) {
@tatsuyax25
tatsuyax25 / numberOfSubstrings.js
Created November 15, 2025 17:48
You are given a binary string s. Return the number of substrings with dominant ones. A string has dominant ones if the number of ones in the string is greater than or equal to the square of the number of zeros in the string.
/**
* @param {string} s
* @return {number}
*/
let numberOfSubstrings = function (s) {
const n = s.length;
// dp[i] stores the nearest index <= i where a '0' occurs
// (or -1 if none). This helps us quickly jump backwards
// to substrings that include more zeros.
@tatsuyax25
tatsuyax25 / rangeAddQueries .js
Created November 14, 2025 17:17
You are given a positive integer n, indicating that we initially have an n x n 0-indexed integer matrix mat filled with zeroes. You are also given a 2D integer array query. For each query[i] = [row1i, col1i, row2i, col2i], you should do the followin
/**
* @param {number} n
* @param {number[][]} queries
* @return {number[][]}
*/
var rangeAddQueries = function(n, queries) {
// Step 1: Initialize an n x n matrix filled with 0s
let mat = Array.from({ length: n }, () => Array(n).fill(0));
// Step 2: Process each query
@tatsuyax25
tatsuyax25 / maxOperations.js
Created November 13, 2025 17:22
You are given a binary string s. You can perform the following operation on the string any number of times: Choose any index i from the string where i + 1 < s.length such that s[i] == '1' and s[i + 1] == '0'. Move the character s[i] to the right un
/**
* Calculates the maximum number of operations that can be performed
* on a binary string `s`. An operation is defined as pairing a '1'
* with the end of a block of consecutive '0's.
*
* @param {string} s - Input binary string consisting of '0' and '1'
* @return {number} - Maximum number of operations possible
*/
var maxOperations = function (s) {
const n = s.length; // Length of the string
@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) => {