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 / minDominoRotations.js
Created May 3, 2025 17:43
In a row of dominoes, tops[i] and bottoms[i] represent the top and bottom halves of the ith domino. (A domino is a tile with two numbers from 1 to 6 - one on each half of the tile.) We may rotate the ith domino, so that tops[i] and bottoms[i] swap v
/**
* Finds the most frequently occurring number in an array.
* @param {number[]} arr - The input array of numbers.
* @return {Object} An object containing the most common item and its count.
*/
function getMostFrequentNumber(arr = []) {
let mostFrequent = arr[0];
let frequencyMap = {};
for (let num of arr) {
@tatsuyax25
tatsuyax25 / pushDominoes.js
Created May 2, 2025 19:56
There are n dominoes in a line, and we place each domino vertically upright. In the beginning, we simultaneously push some of the dominoes either to the left or to the right. After each second, each domino that is falling to the left pushes the adja
/**
* Simulates the domino effect by computing the final state of a row of dominoes.
*
* @param {string} dominoes - A string representing dominoes. 'L' falls left, 'R' falls right, '.' stays upright.
* @return {string} - The final state of the dominoes after all forces are resolved.
*/
var pushDominoes = function(dominoes) {
const n = dominoes.length;
const forces = new Array(n).fill(0);
@tatsuyax25
tatsuyax25 / maxTaskAssign.js
Created May 1, 2025 17:41
You have n tasks and m workers. Each task has a strength requirement stored in a 0-indexed integer array tasks, with the ith task requiring tasks[i] strength to complete. The strength of each worker is stored in a 0-indexed integer array workers, wit
/**
* Determines the maximum number of tasks that can be assigned given workers and pills.
*
* @param {number[]} tasks - Array of task difficulties.
* @param {number[]} workers - Array of worker strengths.
* @param {number} pills - Number of strength-boosting pills available.
* @param {number} strength - Strength increase provided by each pill.
* @return {number} - Maximum number of tasks that can be assigned.
*/
var maxTaskAssign = function(tasks, workers, pills, strength) {
@tatsuyax25
tatsuyax25 / findNumbers.js
Created April 30, 2025 17:19
Given an array nums of integers, return how many of them contain an even number of digits.
/**
* @param {number[]} nums
* @return {number}
*/
var findNumbers = function(nums) {
let count = 0; // Initialize a counter for numbers with an even number of digits
for (let num of nums) { // Loop through each number in the array
// Convert the number to a string (absolute value to handle negatives) and get its length
let digitCount = Math.abs(num).toString().length;
@tatsuyax25
tatsuyax25 / countSubarrays.js
Created April 29, 2025 17:40
You are given an integer array nums and a positive integer k. Return the number of subarrays where the maximum element of nums appears at least k times in that subarray. A subarray is a contiguous sequence of elements within an array.
/**
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
var countSubarrays = function(nums, k) {
// Initialize an array to store the indices of the maximum elements and a variable to store the maximum element
let indices = [], max = 0;
// Iterate over the nums array
@tatsuyax25
tatsuyax25 / countSubarrays.js
Created April 28, 2025 16:24
The score of an array is defined as the product of its sum and its length. For example, the score of [1, 2, 3, 4, 5] is (1 + 2 + 3 + 4 + 5) * 5 = 75. Given a positive integer array nums and an integer k, return the number of non-empty subarrays of n
/**
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
var countSubarrays = function(nums, k) {
let left = 0; // Left pointer of the sliding window
let sum = 0; // Running sum of the current subarray
let result = 0; // Stores the count of valid subarrays
@tatsuyax25
tatsuyax25 / countSubarrays.js
Last active April 27, 2025 21:08
Given an integer array nums, return the number of subarrays of length 3 such that the sum of the first and third numbers equals exactly half of the second number.
/**
* Counts the number of subarrays where the middle element
* is exactly half the sum of the first and last elements.
*
* @param {number[]} nums - An array of numbers.
* @return {number} - The count of valid subarrays.
*/
var countSubarrays = function (nums) {
let count = 0;
@tatsuyax25
tatsuyax25 / countSubarrays.js
Created April 26, 2025 19:50
You are given an integer array nums and two integers minK and maxK. A fixed-bound subarray of nums is a subarray that satisfies the following conditions: The minimum value in the subarray is equal to minK. The maximum value in the subarray is equal
/**
* @param {number[]} nums
* @param {number} minK
* @param {number} maxK
* @return {number}
*/
var countSubarrays = function(nums, minK, maxK) {
// Initialize variables to keep track of indices and result
let minI = -1; // Index of the last occurrence of minK
let maxI = -1; // Index of the last occurrence of maxK
@tatsuyax25
tatsuyax25 / countInterestingSubarrays.js
Created April 25, 2025 16:49
You are given a 0-indexed integer array nums, an integer modulo, and an integer k. Your task is to find the count of subarrays that are interesting. A subarray nums[l..r] is interesting if the following condition holds: Let cnt be the number of in
/**
* @param {number[]} nums
* @param {number} modulo
* @param {number} k
* @return {number}
*/
var countInterestingSubarrays = function(nums, modulo, k) {
let res = 0; // Stores the count of interesting subarrays
let pre = 0; // Prefix sum tracking occurrences of nums[i] % mod === k
let map = new Map([[0, 1]]); // Map to store frequencies of prefix sums modulo 'mod'
@tatsuyax25
tatsuyax25 / countCompleteSubarrays.js
Created April 24, 2025 20:58
You are given an array nums consisting of positive integers. We call a subarray of an array complete if the following condition is satisfied: The number of distinct elements in the subarray is equal to the number of distinct elements in the whole a
/**
* Counts the number of subarrays that contain all distinct elements from the input array.
* @param {number[]} nums - The input array of numbers.
* @return {number} - The count of complete subarrays.
*/
var countCompleteSubarrays = function(nums) {
let count = 0; // Keeps track of the number of complete subarrays
let set = new Set();
// Determine the number of distinct elements in the array