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 / 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) => {
@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.
*/