π¨βπ»
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 |
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 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; |
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} 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 |
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} 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' |
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 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 |