π¨βπ»
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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]` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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 |