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 / minTimeToReach.js
Created May 7, 2025 16:35
There is a dungeon with n x m rooms arranged as a grid. You are given a 2D array moveTime of size n x m, where moveTime[i][j] represents the minimum time in seconds when you can start moving to that room. You start from the room (0, 0) at time t = 0
/**
* Finds the minimum time required to reach the bottom-right cell of the grid.
* Each cell contains a time constraint that dictates the earliest possible arrival.
*
* @param {number[][]} moveTime - A 2D array representing the minimum time required
* to step into each cell.
* @return {number} - The minimum time required to reach the last cell, or -1 if unreachable.
*/
var minTimeToReach = function(moveTime) {
const rows = moveTime.length;
@tatsuyax25
tatsuyax25 / buildArray.js
Created May 6, 2025 17:29
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where ans[i] = nums[nums[i]] for each 0 <= i < nums.length and return it. A zero-based permutation nums is an array of distinct integers from 0 to nums.length - 1
/**
* @param {number[]} nums
* @return {number[]}
*/
var buildArray = function(nums) {
let ans = []; // Initialize an empty array to store the result
// Iterate through each index in the `nums` array
for (let i = 0; i < nums.length; i++) {
ans.push(nums[nums[i]]); // Push the element from `nums` at the index specified by `nums[i]`
@tatsuyax25
tatsuyax25 / numTilings.js
Last active May 5, 2025 17:21
You have two types of tiles: a 2 x 1 domino shape and a tromino shape. You may rotate these shapes. Given an integer n, return the number of ways to tile an 2 x n board. Since the answer may be very large, return it modulo 109 + 7. In a tiling, eve
/**
* Calculates the number of ways to tile a 2 Γ— n board using dominos and trominos.
* @param {number} n - The width of the board.
* @return {number} - The number of valid tilings, modulo 10^9 + 7.
*/
var numTilings = function (n) {
const MOD = 1e9 + 7; // Modulo constraint to prevent overflow
// Base cases for small values of n
if (n === 1) return 1; // Only one vertical domino fits
@tatsuyax25
tatsuyax25 / numEquivDominoPairs.js
Created May 4, 2025 17:34
Given a list of dominoes, dominoes[i] = [a, b] is equivalent to dominoes[j] = [c, d] if and only if either (a == c and b == d), or (a == d and b == c) - that is, one domino can be rotated to be equal to another domino. Return the number of pairs (i,
/**
* Counts the number of equivalent domino pairs.
* Two dominoes are equivalent if they have the same numbers, regardless of order.
*
* @param {number[][]} dominoes - A list of domino pairs represented as 2-element arrays.
* @return {number} - The total number of equivalent domino pairs.
*/
var numEquivDominoPairs = function(dominoes) {
// If there are no dominoes, return 0
if (!dominoes || dominoes.length === 0) {
@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