Skip to content

Instantly share code, notes, and snippets.

View tatsuyax25's full-sized avatar
πŸ‘¨β€πŸ’»
Focusing

Miguel Urena tatsuyax25

πŸ‘¨β€πŸ’»
Focusing
View GitHub Profile
@tatsuyax25
tatsuyax25 / minZeroArray.js
Last active March 13, 2025 20:09
You are given an integer array nums of length n and a 2D array queries where queries[i] = [li, ri, vali]. Each queries[i] represents the following action on nums: Decrement the value at each index in the range [li, ri] in nums by at most vali. The
/**
* Checks if the array can be made zero with a given number of queries.
* @param {number[]} nums - The array of numbers.
* @param {number[][]} queries - The array of queries.
* @param {number} limit - The number of queries to use.
* @return {boolean} - Returns true if the array can be made zero, false otherwise.
*/
let check = (nums, queries, limit) => {
let arr = new Array(nums.length + 1).fill(0);
@tatsuyax25
tatsuyax25 / maximumCount.js
Created March 12, 2025 16:27
Given an array nums sorted in non-decreasing order, return the maximum between the number of positive integers and the number of negative integers. In other words, if the number of positive integers in nums is pos and the number of negative integers
/**
* @param {number[]} nums
* @return {number}
*/
var maximumCount = function(nums) {
// Initialize counters for positive and negative integers
let posCount = 0;
let negCount = 0;
// Iterate through the array
@tatsuyax25
tatsuyax25 / numberOfSubstrings.js
Created March 11, 2025 18:56
Given a string s consisting only of characters a, b and c. Return the number of substrings containing at least one occurrence of all these characters a, b and c.
/**
* @param {string} s - Input string containing characters 'a', 'b', and 'c'
* @return {number} - Number of substrings containing at least one 'a', 'b', and 'c'
*/
var numberOfSubstrings = function(s) {
let count = 0; // To keep track of the number of unique characters 'a', 'b', and 'c' in the current window
let map = {'a': 0, 'b': 0, 'c': 0}; // Map to store the count of each character 'a', 'b', and 'c'
let res = 0; // To store the result (number of substrings)
let l = 0; // Left pointer for the sliding window
@tatsuyax25
tatsuyax25 / countOfSubstrings.js
Created March 10, 2025 20:20
You are given a string word and a non-negative integer k. Return the total number of substrings of word that contain every vowel ('a', 'e', 'i', 'o', and 'u') at least once and exactly k consonants.
/**
* @param {string} word
* @param {number} k
* @return {number}
*/
var countOfSubstrings = function(word, k) {
// Function to count substrings with at least k consonants
function atLeastK(word, k) {
const vowels = new Set(['a', 'e', 'i', 'o', 'u']);
@tatsuyax25
tatsuyax25 / numberOfAlternatingGroups.js
Created March 9, 2025 18:30
There is a circle of red and blue tiles. You are given an array of integers colors and an integer k. The color of tile i is represented by colors[i]: colors[i] == 0 means that tile i is red. colors[i] == 1 means that tile i is blue. An alternating g
/**
* @param {number[]} colors
* @param {number} k
* @return {number}
*/
var numberOfAlternatingGroups = function(colors, k) {
// Extend the colors array to handle the circular nature by appending the first (k - 1) elements to the end
colors.push(...colors.slice(0, k - 1));
let count = 0; // Counter for the number of alternating groups
@tatsuyax25
tatsuyax25 / minimumRecolors.js
Last active March 8, 2025 18:44
You are given a 0-indexed string blocks of length n, where blocks[i] is either 'W' or 'B', representing the color of the ith block. The characters 'W' and 'B' denote the colors white and black, respectively. You are also given an integer k, which is
/**
* @param {string} blocks
* @param {number} k
* @return {number}
*/
var minimumRecolors = function(blocks, k) {
const n = blocks.length;
let whiteBlocks = 0;
// Initialize the count of white blocks in the first window of length k
@tatsuyax25
tatsuyax25 / closestPrimes.js
Created March 7, 2025 21:15
Given two positive integers left and right, find the two integers num1 and num2 such that: left <= num1 < num2 <= right . Both num1 and num2 are prime numbers. num2 - num1 is the minimum amongst all other pairs satisfying the above conditions. Retur
/**
* @param {number} left
* @param {number} right
* @return {number[]}
*/
// Function to check if a number is prime
function isPrime(num) {
if (num <= 1) return false;
if (num <= 3) return true;
if (num % 2 === 0 || num % 3 === 0) return false;
@tatsuyax25
tatsuyax25 / findMissingAndRepeatedValues.js
Created March 6, 2025 17:34
You are given a 0-indexed 2D integer matrix grid of size n * n with values in the range [1, n2]. Each integer appears exactly once except a which appears twice and b which is missing. The task is to find the repeating and missing numbers a and b. Re
/**
* @param {number[][]} grid
* @return {number[]}
*/
var findMissingAndRepeatedValues = function(grid) {
const n = grid.length;
const expectedSum = (n * n * (n * n + 1)) / 2;
let actualSum = 0;
const numCounts = {};
let repeating = -1;
@tatsuyax25
tatsuyax25 / coloredCells.js
Created March 5, 2025 17:53
There exists an infinitely large two-dimensional grid of uncolored unit cells. You are given a positive integer n, indicating that you must do the following routine for n minutes: At the first minute, color any arbitrary unit cell blue. Every minute
/**
* @param {number} n
* @return {number}
*/
var coloredCells = function(n) {
// This function calculates the number of blue cells in the grid after 'n' minutes.
// The formula is derived as the sum of two squares:
// (n * n) represents the number of cells in an n x n grid, which is the inner square.
// ((n - 1) * (n - 1)) represents the number of cells in an (n-1) x (n-1) grid, which is the outer ring.
return (n * n) + ((n - 1) * (n - 1));
@tatsuyax25
tatsuyax25 / checkPowersOfThree.js
Created March 4, 2025 17:07
Given an integer n, return true if it is possible to represent n as the sum of distinct powers of three. Otherwise, return false. An integer y is a power of three if there exists an integer x such that y == 3x.
/**
* @param {number} n
* @return {boolean}
*/
var checkPowersOfThree = function(n) {
// Loop until n becomes zero
while (n > 0) {
// If the remainder when n is divided by 3 is 2, return. false
// This means n cannot be represented as the sum of distinct powers of three
if (n % 3 === 2) {