π¨βπ»
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 {string} s | |
* @param {number} t | |
* @return {number} | |
*/ | |
var lengthAfterTransformations = function(s, t) { | |
const MOD = 1000000007; | |
const n = s.length; | |
// Step 1: Count occurrences of each character in 's' |
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 all three-digit even numbers that can be formed using the given digits. | |
* @param {number[]} digits - An array of digits (0-9). | |
* @return {number[]} - An array of unique three-digit even numbers. | |
*/ | |
var findEvenNumbers = function(digits) { | |
const arr = []; // Stores valid even numbers | |
const ct = Array(10).fill(0); // Count occurrences of each digit (0-9) | |
// Count frequency of digits in the input 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
/** | |
* Function to determine the maximum possible sum by replacing zeros | |
* in either of the given arrays. | |
* | |
* @param {number[]} nums1 - First array of numbers | |
* @param {number[]} nums2 - Second array of numbers | |
* @return {number} - Maximum possible sum or -1 if impossible | |
*/ | |
var minSum = function(nums1, nums2) { | |
let zeroCount1 = 0; // Count of zeros in nums1 |
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
/** | |
* Function to count the number of distinct balanced permutations of a given number string. | |
* A permutation is balanced if the sum of digits at even indices equals the sum at odd indices. | |
* | |
* @param {string} num - Input string consisting of digits. | |
* @returns {number} - Number of balanced permutations modulo 10^9 + 7. | |
*/ | |
var countBalancedPermutations = function(num) { | |
const MOD = 1000000007; | |
const n = num.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[][]} moveTime | |
* @return {number} | |
*/ | |
var minTimeToReach = function(moveTime) { | |
const n = moveTime.length, m = moveTime[0].length; | |
// Distance array initialized with Infinity, representing minimum time to reach each cell | |
const d = Array.from({ length: n }, () => Array(m).fill(Infinity)); |
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) { |
NewerOlder