From 6936555bfc6860c67e4130af2bde442ac20622ba Mon Sep 17 00:00:00 2001 From: Josh Crozier <5490537+JoshCrozier@users.noreply.github.com> Date: Thu, 9 Oct 2025 08:20:25 -0500 Subject: [PATCH 01/23] Add solution #3494 --- README.md | 1 + ...-minimum-amount-of-time-to-brew-potions.js | 37 +++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 solutions/3494-find-the-minimum-amount-of-time-to-brew-potions.js diff --git a/README.md b/README.md index 6947142b..ee885835 100644 --- a/README.md +++ b/README.md @@ -2760,6 +2760,7 @@ 3484|[Design Spreadsheet](./solutions/3484-design-spreadsheet.js)|Medium| 3487|[Maximum Unique Subarray Sum After Deletion](./solutions/3487-maximum-unique-subarray-sum-after-deletion.js)|Easy| 3491|[Phone Number Prefix](./solutions/3491-phone-number-prefix.js)|Easy| +3494|[Find the Minimum Amount of Time to Brew Potions](./solutions/3494-find-the-minimum-amount-of-time-to-brew-potions.js)|Medium| 3495|[Minimum Operations to Make Array Elements Zero](./solutions/3495-minimum-operations-to-make-array-elements-zero.js)|Hard| 3496|[Maximize Score After Pair Deletions](./solutions/3496-maximize-score-after-pair-deletions.js)|Medium| 3506|[Find Time Required to Eliminate Bacterial Strains](./solutions/3506-find-time-required-to-eliminate-bacterial-strains.js)|Hard| diff --git a/solutions/3494-find-the-minimum-amount-of-time-to-brew-potions.js b/solutions/3494-find-the-minimum-amount-of-time-to-brew-potions.js new file mode 100644 index 00000000..1ddd6793 --- /dev/null +++ b/solutions/3494-find-the-minimum-amount-of-time-to-brew-potions.js @@ -0,0 +1,37 @@ +/** + * 3494. Find the Minimum Amount of Time to Brew Potions + * https://leetcode.com/problems/find-the-minimum-amount-of-time-to-brew-potions/ + * Difficulty: Medium + * + * You are given two integer arrays, skill and mana, of length n and m, respectively. + * + * In a laboratory, n wizards must brew m potions in order. Each potion has a mana capacity + * mana[j] and must pass through all the wizards sequentially to be brewed properly. The + * time taken by the ith wizard on the jth potion is timeij = skill[i] * mana[j]. + * + * Since the brewing process is delicate, a potion must be passed to the next wizard + * immediately after the current wizard completes their work. This means the timing must + * be synchronized so that each wizard begins working on a potion exactly when it arrives. + * + * Return the minimum amount of time required for the potions to be brewed properly. + */ + +/** + * @param {number[]} skill + * @param {number[]} mana + * @return {number} + */ +var minTime = function(skill, mana) { + const result = new Array(skill.length + 1).fill(0); + + for (let j = 0; j < mana.length; j++) { + for (let i = 0; i < skill.length; i++) { + result[i + 1] = Math.max(result[i + 1], result[i]) + mana[j] * skill[i]; + } + for (let i = skill.length - 1; i > 0; i--) { + result[i] = result[i + 1] - mana[j] * skill[i]; + } + } + + return result[skill.length]; +}; From c41ac67d68f16eea9784ff51b81a1a86d7bd70a9 Mon Sep 17 00:00:00 2001 From: Josh Crozier <5490537+JoshCrozier@users.noreply.github.com> Date: Fri, 10 Oct 2025 15:09:43 -0500 Subject: [PATCH 02/23] Add solution #3147 --- README.md | 1 + ...-maximum-energy-from-the-mystic-dungeon.js | 39 +++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 solutions/3147-taking-maximum-energy-from-the-mystic-dungeon.js diff --git a/README.md b/README.md index ee885835..f2eb9a83 100644 --- a/README.md +++ b/README.md @@ -2660,6 +2660,7 @@ 3135|[Equalize Strings by Adding or Removing Characters at Ends](./solutions/3135-equalize-strings-by-adding-or-removing-characters-at-ends.js)|Medium| 3136|[Valid Word](./solutions/3136-valid-word.js)|Easy| 3141|[Maximum Hamming Distances](./solutions/3141-maximum-hamming-distances.js)|Hard| +3147|[Taking Maximum Energy From the Mystic Dungeon](./solutions/3147-taking-maximum-energy-from-the-mystic-dungeon.js)|Medium| 3151|[Special Array I](./solutions/3151-special-array-i.js)|Easy| 3155|[Maximum Number of Upgradable Servers](./solutions/3155-maximum-number-of-upgradable-servers.js)|Medium| 3157|[Find the Level of Tree with Minimum Sum](./solutions/3157-find-the-level-of-tree-with-minimum-sum.js)|Medium| diff --git a/solutions/3147-taking-maximum-energy-from-the-mystic-dungeon.js b/solutions/3147-taking-maximum-energy-from-the-mystic-dungeon.js new file mode 100644 index 00000000..e977aecd --- /dev/null +++ b/solutions/3147-taking-maximum-energy-from-the-mystic-dungeon.js @@ -0,0 +1,39 @@ +/** + * 3147. Taking Maximum Energy From the Mystic Dungeon + * https://leetcode.com/problems/taking-maximum-energy-from-the-mystic-dungeon/ + * Difficulty: Medium + * + * In a mystic dungeon, n magicians are standing in a line. Each magician has an attribute + * that gives you energy. Some magicians can give you negative energy, which means taking + * energy from you. + * + * You have been cursed in such a way that after absorbing energy from magician i, you will + * be instantly transported to magician (i + k). This process will be repeated until you + * reach the magician where (i + k) does not exist. + * + * In other words, you will choose a starting point and then teleport with k jumps until you + * reach the end of the magicians' sequence, absorbing all the energy during the journey. + * + * You are given an array energy and an integer k. Return the maximum possible energy you can gain. + * + * Note that when you are reach a magician, you must take energy from them, whether it is negative + * or positive energy. + */ + +/** + * @param {number[]} energy + * @param {number} k + * @return {number} + */ +var maximumEnergy = function(energy, k) { + const n = energy.length; + const dp = new Array(n).fill(0); + let result = -Infinity; + + for (let i = n - 1; i >= 0; i--) { + dp[i] = energy[i] + (i + k < n ? dp[i + k] : 0); + result = Math.max(result, dp[i]); + } + + return result; +}; From 37d131454bf01ad73dd0d41aa978b7053370f316 Mon Sep 17 00:00:00 2001 From: Josh Crozier <5490537+JoshCrozier@users.noreply.github.com> Date: Sat, 11 Oct 2025 15:50:47 -0500 Subject: [PATCH 03/23] Add solution #3186 --- README.md | 1 + ...maximum-total-damage-with-spell-casting.js | 55 +++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 solutions/3186-maximum-total-damage-with-spell-casting.js diff --git a/README.md b/README.md index f2eb9a83..269ce1cc 100644 --- a/README.md +++ b/README.md @@ -2672,6 +2672,7 @@ 3174|[Clear Digits](./solutions/3174-clear-digits.js)|Easy| 3178|[Find the Child Who Has the Ball After K Seconds](./solutions/3178-find-the-child-who-has-the-ball-after-k-seconds.js)|Easy| 3183|[The Number of Ways to Make the Sum](./solutions/3183-the-number-of-ways-to-make-the-sum.js)|Medium| +3186|[Maximum Total Damage With Spell Casting](./solutions/3186-maximum-total-damage-with-spell-casting.js)|Medium| 3189|[Minimum Moves to Get a Peaceful Board](./solutions/3189-minimum-moves-to-get-a-peaceful-board.js)|Medium| 3191|[Minimum Operations to Make Binary Array Elements Equal to One I](./solutions/3191-minimum-operations-to-make-binary-array-elements-equal-to-one-i.js)|Medium| 3195|[Find the Minimum Area to Cover All Ones I](./solutions/3195-find-the-minimum-area-to-cover-all-ones-i.js)|Medium| diff --git a/solutions/3186-maximum-total-damage-with-spell-casting.js b/solutions/3186-maximum-total-damage-with-spell-casting.js new file mode 100644 index 00000000..c392c9ec --- /dev/null +++ b/solutions/3186-maximum-total-damage-with-spell-casting.js @@ -0,0 +1,55 @@ +/** + * 3186. Maximum Total Damage With Spell Casting + * https://leetcode.com/problems/maximum-total-damage-with-spell-casting/ + * Difficulty: Medium + * + * A magician has various spells. + * + * You are given an array power, where each element represents the damage of a spell. Multiple + * spells can have the same damage value. + * + * It is a known fact that if a magician decides to cast a spell with a damage of power[i], + * they cannot cast any spell with a damage of power[i] - 2, power[i] - 1, power[i] + 1, or + * power[i] + 2. + * + * Each spell can be cast only once. + * + * Return the maximum possible total damage that a magician can cast. + */ + +/** + * @param {number[]} power + * @return {number} + */ +var maximumTotalDamage = function(power) { + const frequency = new Map(); + for (const p of power) { + frequency.set(p, (frequency.get(p) || 0) + 1); + } + + const uniquePowers = Array.from(frequency.keys()).sort((a, b) => a - b); + const n = uniquePowers.length; + + if (n === 0) return 0; + if (n === 1) return uniquePowers[0] * frequency.get(uniquePowers[0]); + + const dp = new Array(n).fill(0); + dp[0] = uniquePowers[0] * frequency.get(uniquePowers[0]); + + for (let i = 1; i < n; i++) { + const currentPower = uniquePowers[i]; + const currentDamage = currentPower * frequency.get(currentPower); + + let j = i - 1; + while (j >= 0 && uniquePowers[j] >= currentPower - 2) { + j--; + } + + const withCurrent = currentDamage + (j >= 0 ? dp[j] : 0); + const withoutCurrent = dp[i - 1]; + + dp[i] = Math.max(withCurrent, withoutCurrent); + } + + return dp[n - 1]; +}; From 466db0525d63b418cdb3da27196248467f9ae4e4 Mon Sep 17 00:00:00 2001 From: Josh Crozier <5490537+JoshCrozier@users.noreply.github.com> Date: Tue, 14 Oct 2025 18:26:58 -0500 Subject: [PATCH 04/23] Add solution #3349 --- README.md | 1 + ...jacent-increasing-subarrays-detection-i.js | 36 +++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 solutions/3349-adjacent-increasing-subarrays-detection-i.js diff --git a/README.md b/README.md index 269ce1cc..66912aeb 100644 --- a/README.md +++ b/README.md @@ -2712,6 +2712,7 @@ 3342|[Find Minimum Time to Reach Last Room II](./solutions/3342-find-minimum-time-to-reach-last-room-ii.js)|Medium| 3343|[Count Number of Balanced Permutations](./solutions/3343-count-number-of-balanced-permutations.js)|Hard| 3344|[Maximum Sized Array](./solutions/3344-maximum-sized-array.js)|Medium| +3349|[Adjacent Increasing Subarrays Detection I](./solutions/3349-adjacent-increasing-subarrays-detection-i.js)|Easy| 3353|[Minimum Total Operations](./solutions/3353-minimum-total-operations.js)|Easy| 3355|[Zero Array Transformation I](./solutions/3355-zero-array-transformation-i.js)|Medium| 3356|[Zero Array Transformation II](./solutions/3356-zero-array-transformation-ii.js)|Medium| diff --git a/solutions/3349-adjacent-increasing-subarrays-detection-i.js b/solutions/3349-adjacent-increasing-subarrays-detection-i.js new file mode 100644 index 00000000..e2af9ede --- /dev/null +++ b/solutions/3349-adjacent-increasing-subarrays-detection-i.js @@ -0,0 +1,36 @@ +/** + * 3349. Adjacent Increasing Subarrays Detection I + * https://leetcode.com/problems/adjacent-increasing-subarrays-detection-i/ + * Difficulty: Easy + * + * Given an array nums of n integers and an integer k, determine whether there exist two + * adjacent subarrays of length k such that both subarrays are strictly increasing. + * Specifically, check if there are two subarrays starting at indices a and b (a < b), + * where: + * - Both subarrays nums[a..a + k - 1] and nums[b..b + k - 1] are strictly increasing. + * - The subarrays must be adjacent, meaning b = a + k. + * + * Return true if it is possible to find two such subarrays, and false otherwise. + */ + +/** + * @param {number[]} nums + * @param {number} k + * @return {boolean} + */ +var hasIncreasingSubarrays = function(nums, k) { + for (let i = 0; i <= nums.length - 2 * k; i++) { + if (helper(i, k) && helper(i + k, k)) { + return true; + } + } + + return false; + + function helper(start, length) { + for (let i = start; i < start + length - 1; i++) { + if (nums[i] >= nums[i + 1]) return false; + } + return true; + } +}; From 6a5e0660f7b4a4f6a7ce61c0a78e0123a5292c35 Mon Sep 17 00:00:00 2001 From: Josh Crozier <5490537+JoshCrozier@users.noreply.github.com> Date: Wed, 15 Oct 2025 18:21:45 -0500 Subject: [PATCH 05/23] Add solution #3539 --- README.md | 1 + ...m-of-array-product-of-magical-sequences.js | 101 ++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 solutions/3539-find-sum-of-array-product-of-magical-sequences.js diff --git a/README.md b/README.md index 66912aeb..9bd6d005 100644 --- a/README.md +++ b/README.md @@ -2772,6 +2772,7 @@ 3516|[Find Closest Person](./solutions/3516-find-closest-person.js)|Easy| 3520|[Minimum Threshold for Inversion Pairs Count](./solutions/3520-minimum-threshold-for-inversion-pairs-count.js)|Medium| 3535|[Unit Conversion II](./solutions/3535-unit-conversion-ii.js)|Medium| +3539|[Find Sum of Array Product of Magical Sequences](./solutions/3539-find-sum-of-array-product-of-magical-sequences.js)|Hard| 3541|[Find Most Frequent Vowel and Consonant](./solutions/3541-find-most-frequent-vowel-and-consonant.js)|Easy| ## License diff --git a/solutions/3539-find-sum-of-array-product-of-magical-sequences.js b/solutions/3539-find-sum-of-array-product-of-magical-sequences.js new file mode 100644 index 00000000..5e7fd749 --- /dev/null +++ b/solutions/3539-find-sum-of-array-product-of-magical-sequences.js @@ -0,0 +1,101 @@ +/** + * 3539. Find Sum of Array Product of Magical Sequences + * https://leetcode.com/problems/find-sum-of-array-product-of-magical-sequences/ + * Difficulty: Hard + * + * You are given two integers, m and k, and an integer array nums. + * + * A sequence of integers seq is called magical if: + * - seq has a size of m. + * - 0 <= seq[i] < nums.length + * - The binary representation of 2seq[0] + 2seq[1] + ... + 2seq[m - 1] has k set bits. + * + * The array product of this sequence is defined as prod(seq) = (nums[seq[0]] + * * nums[seq[1]] * ... * nums[seq[m - 1]]). + * + * Return the sum of the array products for all valid magical sequences. + * + * Since the answer may be large, return it modulo 109 + 7. + * + * A set bit refers to a bit in the binary representation of a number that has a value of 1. + */ + +/** + * @param {number} m + * @param {number} k + * @param {number[]} nums + * @return {number} + */ +var magicalSum = function(m, k, nums) { + const MOD = 1000000007n; + const map = new Map(); + const n = nums.length; + + function bitCount(num) { + let count = 0; + while (num > 0) { + count += num & 1; + num >>= 1; + } + return count; + } + + function modPow(base, exp) { + let result = 1n; + base = BigInt(base) % MOD; + let e = BigInt(exp); + while (e > 0n) { + if (e & 1n) result = (result * base) % MOD; + base = (base * base) % MOD; + e >>= 1n; + } + return result; + } + + const factorialCache = [1n]; + function factorial(n) { + while (factorialCache.length <= n) { + factorialCache.push( + factorialCache[factorialCache.length - 1] * BigInt(factorialCache.length) + ); + } + return factorialCache[n]; + } + + function comb(n, r) { + if (r > n || r < 0) return 0n; + if (r === 0 || r === n) return 1n; + return factorial(n) / (factorial(r) * factorial(n - r)); + } + + function dfs(remaining, oddNeeded, index, carry) { + if (remaining < 0 || oddNeeded < 0 || remaining + bitCount(carry) < oddNeeded) { + return 0n; + } + if (remaining === 0) { + return bitCount(carry) === oddNeeded ? 1n : 0n; + } + if (index >= n) { + return 0n; + } + + const key = `${remaining},${oddNeeded},${index},${carry}`; + if (map.has(key)) return map.get(key); + + let result = 0n; + for (let take = 0; take <= remaining; take++) { + const ways = (comb(remaining, take) * modPow(nums[index], take)) % MOD; + const newCarry = carry + take; + const contribution = dfs( + remaining - take, oddNeeded - (newCarry % 2), + index + 1, Math.floor(newCarry / 2), + ); + result = (result + ways * contribution) % MOD; + } + + map.set(key, result); + return result; + } + + return Number(dfs(m, k, 0, 0)); +}; From fdca9a447d55db6f2f07a203a291dc71ce2f65dd Mon Sep 17 00:00:00 2001 From: Josh Crozier <5490537+JoshCrozier@users.noreply.github.com> Date: Wed, 15 Oct 2025 18:38:37 -0500 Subject: [PATCH 06/23] Add solution #3350 --- README.md | 1 + ...acent-increasing-subarrays-detection-ii.js | 45 +++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 solutions/3350-adjacent-increasing-subarrays-detection-ii.js diff --git a/README.md b/README.md index 9bd6d005..181c8a20 100644 --- a/README.md +++ b/README.md @@ -2713,6 +2713,7 @@ 3343|[Count Number of Balanced Permutations](./solutions/3343-count-number-of-balanced-permutations.js)|Hard| 3344|[Maximum Sized Array](./solutions/3344-maximum-sized-array.js)|Medium| 3349|[Adjacent Increasing Subarrays Detection I](./solutions/3349-adjacent-increasing-subarrays-detection-i.js)|Easy| +3350|[Adjacent Increasing Subarrays Detection II](./solutions/3350-adjacent-increasing-subarrays-detection-ii.js)|Medium| 3353|[Minimum Total Operations](./solutions/3353-minimum-total-operations.js)|Easy| 3355|[Zero Array Transformation I](./solutions/3355-zero-array-transformation-i.js)|Medium| 3356|[Zero Array Transformation II](./solutions/3356-zero-array-transformation-ii.js)|Medium| diff --git a/solutions/3350-adjacent-increasing-subarrays-detection-ii.js b/solutions/3350-adjacent-increasing-subarrays-detection-ii.js new file mode 100644 index 00000000..7f103b4a --- /dev/null +++ b/solutions/3350-adjacent-increasing-subarrays-detection-ii.js @@ -0,0 +1,45 @@ +/** + * 3350. Adjacent Increasing Subarrays Detection II + * https://leetcode.com/problems/adjacent-increasing-subarrays-detection-ii/ + * Difficulty: Medium + * + * Given an array nums of n integers, your task is to find the maximum value of k for which + * there exist two adjacent subarrays of length k each, such that both subarrays are strictly + * increasing. Specifically, check if there are two subarrays of length k starting at indices + * a and b (a < b), where: + * - Both subarrays nums[a..a + k - 1] and nums[b..b + k - 1] are strictly increasing. + * - The subarrays must be adjacent, meaning b = a + k. + * + * Return the maximum possible value of k. + * + * A subarray is a contiguous non-empty sequence of elements within an array. + */ + +/** + * @param {number[]} nums + * @return {number} + */ +var maxIncreasingSubarrays = function(nums) { + const n = nums.length; + const lengths = new Array(n).fill(1); + + for (let i = n - 2; i >= 0; i--) { + if (nums[i] < nums[i + 1]) { + lengths[i] = lengths[i + 1] + 1; + } + } + + let result = 0; + for (let i = 0; i < n; i++) { + const currentLength = lengths[i]; + result = Math.max(result, Math.floor(currentLength / 2)); + + const nextIndex = i + currentLength; + if (nextIndex < n) { + const minLength = Math.min(currentLength, lengths[nextIndex]); + result = Math.max(result, minLength); + } + } + + return result; +}; From 1990d22143a97cca24f7e0e02c070583e0574da2 Mon Sep 17 00:00:00 2001 From: Josh Crozier <5490537+JoshCrozier@users.noreply.github.com> Date: Wed, 22 Oct 2025 18:46:14 -0500 Subject: [PATCH 07/23] Add solution #3347 --- README.md | 1 + ...-element-after-performing-operations-ii.js | 62 +++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 solutions/3347-maximum-frequency-of-an-element-after-performing-operations-ii.js diff --git a/README.md b/README.md index 181c8a20..1e992959 100644 --- a/README.md +++ b/README.md @@ -2712,6 +2712,7 @@ 3342|[Find Minimum Time to Reach Last Room II](./solutions/3342-find-minimum-time-to-reach-last-room-ii.js)|Medium| 3343|[Count Number of Balanced Permutations](./solutions/3343-count-number-of-balanced-permutations.js)|Hard| 3344|[Maximum Sized Array](./solutions/3344-maximum-sized-array.js)|Medium| +3347|[Maximum Frequency of an Element After Performing Operations II](./solutions/3347-maximum-frequency-of-an-element-after-performing-operations-ii.js)|Hard| 3349|[Adjacent Increasing Subarrays Detection I](./solutions/3349-adjacent-increasing-subarrays-detection-i.js)|Easy| 3350|[Adjacent Increasing Subarrays Detection II](./solutions/3350-adjacent-increasing-subarrays-detection-ii.js)|Medium| 3353|[Minimum Total Operations](./solutions/3353-minimum-total-operations.js)|Easy| diff --git a/solutions/3347-maximum-frequency-of-an-element-after-performing-operations-ii.js b/solutions/3347-maximum-frequency-of-an-element-after-performing-operations-ii.js new file mode 100644 index 00000000..fdbe1898 --- /dev/null +++ b/solutions/3347-maximum-frequency-of-an-element-after-performing-operations-ii.js @@ -0,0 +1,62 @@ +/** + * 3347. Maximum Frequency of an Element After Performing Operations II + * https://leetcode.com/problems/maximum-frequency-of-an-element-after-performing-operations-ii + * Difficulty: Hard + * + * You are given an integer array nums and two integers k and numOperations. + * + * You must perform an operation numOperations times on nums, where in each operation you: + * - Select an index i that was not selected in any previous operations. + * - Add an integer in the range [-k, k] to nums[i]. + * + * Return the maximum possible frequency of any element in nums after performing the operations. + */ + +/** + * @param {number[]} nums + * @param {number} k + * @param {number} numOperations + * @return {number} + */ +var maxFrequency = function(nums, k, numOperations) { + const n = nums.length; + nums.sort((a, b) => a - b); + + const count = new Map(); + for (const num of nums) { + count.set(num, (count.get(num) || 0) + 1); + } + + let result = 0; + let left = 0; + let right = 0; + for (let mid = 0; mid < n; mid++) { + while (nums[mid] - nums[left] > k) { + left++; + } + + while (right < n - 1 && nums[right + 1] - nums[mid] <= k) { + right++; + } + + const total = right - left + 1; + result = Math.max( + result, + Math.min(total - count.get(nums[mid]), numOperations) + count.get(nums[mid]) + ); + } + + left = 0; + for (right = 0; right < n; right++) { + let mid = Math.floor((nums[left] + nums[right]) / 2); + + while (mid - nums[left] > k || nums[right] - mid > k) { + left++; + mid = Math.floor((nums[left] + nums[right]) / 2); + } + + result = Math.max(result, Math.min(right - left + 1, numOperations)); + } + + return result; +}; From 637f5210d0098476886267ed83f098f5d0ad05f7 Mon Sep 17 00:00:00 2001 From: Josh Crozier <5490537+JoshCrozier@users.noreply.github.com> Date: Wed, 22 Oct 2025 18:47:06 -0500 Subject: [PATCH 08/23] Add solution #3346 --- README.md | 1 + ...n-element-after-performing-operations-i.js | 62 +++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 solutions/3346-maximum-frequency-of-an-element-after-performing-operations-i.js diff --git a/README.md b/README.md index 1e992959..4e20e6ae 100644 --- a/README.md +++ b/README.md @@ -2712,6 +2712,7 @@ 3342|[Find Minimum Time to Reach Last Room II](./solutions/3342-find-minimum-time-to-reach-last-room-ii.js)|Medium| 3343|[Count Number of Balanced Permutations](./solutions/3343-count-number-of-balanced-permutations.js)|Hard| 3344|[Maximum Sized Array](./solutions/3344-maximum-sized-array.js)|Medium| +3346|[Maximum Frequency of an Element After Performing Operations I](./solutions/3346-maximum-frequency-of-an-element-after-performing-operations-i.js)|Medium| 3347|[Maximum Frequency of an Element After Performing Operations II](./solutions/3347-maximum-frequency-of-an-element-after-performing-operations-ii.js)|Hard| 3349|[Adjacent Increasing Subarrays Detection I](./solutions/3349-adjacent-increasing-subarrays-detection-i.js)|Easy| 3350|[Adjacent Increasing Subarrays Detection II](./solutions/3350-adjacent-increasing-subarrays-detection-ii.js)|Medium| diff --git a/solutions/3346-maximum-frequency-of-an-element-after-performing-operations-i.js b/solutions/3346-maximum-frequency-of-an-element-after-performing-operations-i.js new file mode 100644 index 00000000..3bc5e43c --- /dev/null +++ b/solutions/3346-maximum-frequency-of-an-element-after-performing-operations-i.js @@ -0,0 +1,62 @@ +/** + * 3346. Maximum Frequency of an Element After Performing Operations I + * https://leetcode.com/problems/maximum-frequency-of-an-element-after-performing-operations-i/ + * Difficulty: Medium + * + * You are given an integer array nums and two integers k and numOperations. + * + * You must perform an operation numOperations times on nums, where in each operation you: + * - Select an index i that was not selected in any previous operations. + * - Add an integer in the range [-k, k] to nums[i]. + * + * Return the maximum possible frequency of any element in nums after performing the operations. + */ + +/** + * @param {number[]} nums + * @param {number} k + * @param {number} numOperations + * @return {number} + */ +var maxFrequency = function(nums, k, numOperations) { + const n = nums.length; + nums.sort((a, b) => a - b); + + const count = new Map(); + for (const num of nums) { + count.set(num, (count.get(num) || 0) + 1); + } + + let result = 0; + let left = 0; + let right = 0; + for (let mid = 0; mid < n; mid++) { + while (nums[mid] - nums[left] > k) { + left++; + } + + while (right < n - 1 && nums[right + 1] - nums[mid] <= k) { + right++; + } + + const total = right - left + 1; + result = Math.max( + result, + Math.min(total - count.get(nums[mid]), numOperations) + count.get(nums[mid]) + ); + } + + left = 0; + for (right = 0; right < n; right++) { + let mid = Math.floor((nums[left] + nums[right]) / 2); + + while (mid - nums[left] > k || nums[right] - mid > k) { + left++; + mid = Math.floor((nums[left] + nums[right]) / 2); + } + + result = Math.max(result, Math.min(right - left + 1, numOperations)); + } + + return result; +}; From 64ef6176717e0b0928184a15079288ef7c380d22 Mon Sep 17 00:00:00 2001 From: Josh Crozier <5490537+JoshCrozier@users.noreply.github.com> Date: Mon, 27 Oct 2025 19:41:29 -0500 Subject: [PATCH 09/23] Add solution #3354 --- README.md | 1 + .../3354-make-array-elements-equal-to-zero.js | 60 +++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 solutions/3354-make-array-elements-equal-to-zero.js diff --git a/README.md b/README.md index 4e20e6ae..191f1ab9 100644 --- a/README.md +++ b/README.md @@ -2717,6 +2717,7 @@ 3349|[Adjacent Increasing Subarrays Detection I](./solutions/3349-adjacent-increasing-subarrays-detection-i.js)|Easy| 3350|[Adjacent Increasing Subarrays Detection II](./solutions/3350-adjacent-increasing-subarrays-detection-ii.js)|Medium| 3353|[Minimum Total Operations](./solutions/3353-minimum-total-operations.js)|Easy| +3354|[Make Array Elements Equal to Zero](./solutions/3354-make-array-elements-equal-to-zero.js)|Easy| 3355|[Zero Array Transformation I](./solutions/3355-zero-array-transformation-i.js)|Medium| 3356|[Zero Array Transformation II](./solutions/3356-zero-array-transformation-ii.js)|Medium| 3359|[Find Sorted Submatrices With Maximum Element at Most K](./solutions/3359-find-sorted-submatrices-with-maximum-element-at-most-k.js)|Hard| diff --git a/solutions/3354-make-array-elements-equal-to-zero.js b/solutions/3354-make-array-elements-equal-to-zero.js new file mode 100644 index 00000000..865cff41 --- /dev/null +++ b/solutions/3354-make-array-elements-equal-to-zero.js @@ -0,0 +1,60 @@ +/** + * 3354. Make Array Elements Equal to Zero + * https://leetcode.com/problems/make-array-elements-equal-to-zero/ + * Difficulty: Easy + * + * You are given an integer array nums. + * + * Start by selecting a starting position curr such that nums[curr] == 0, and choose a movement + * direction of either left or right. + * + * After that, you repeat the following process: + * - If curr is out of the range [0, n - 1], this process ends. + * - If nums[curr] == 0, move in the current direction by incrementing curr if you are moving + * right, or decrementing curr if you are moving left. + * - Else if nums[curr] > 0: + * - Decrement nums[curr] by 1. + * - Reverse your movement direction (left becomes right and vice versa). + * - Take a step in your new direction. + * + * A selection of the initial position curr and movement direction is considered valid if + * every element in nums becomes 0 by the end of the process. + * + * Return the number of possible valid selections. + */ + +/** + * @param {number[]} nums + * @return {number} + */ +var countValidSelections = function(nums) { + const n = nums.length; + let result = 0; + + for (let i = 0; i < n; i++) { + if (nums[i] === 0) { + if (helper(i, -1)) result++; + if (helper(i, 1)) result++; + } + } + + return result; + + function helper(startIndex, direction) { + const arr = [...nums]; + let current = startIndex; + let dir = direction; + + while (current >= 0 && current < n) { + if (arr[current] === 0) { + current += dir; + } else { + arr[current]--; + dir = -dir; + current += dir; + } + } + + return arr.every(val => val === 0); + } +}; From eff5acd07ff80baba24737eeaf6523b0e52cdc5a Mon Sep 17 00:00:00 2001 From: Josh Crozier <5490537+JoshCrozier@users.noreply.github.com> Date: Tue, 28 Oct 2025 22:08:58 -0500 Subject: [PATCH 10/23] Add solution #3370 --- README.md | 1 + .../3370-smallest-number-with-all-set-bits.js | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 solutions/3370-smallest-number-with-all-set-bits.js diff --git a/README.md b/README.md index 191f1ab9..7260425c 100644 --- a/README.md +++ b/README.md @@ -2724,6 +2724,7 @@ 3362|[Zero Array Transformation III](./solutions/3362-zero-array-transformation-iii.js)|Medium| 3363|[Find the Maximum Number of Fruits Collected](./solutions/3363-find-the-maximum-number-of-fruits-collected.js)|Hard| 3369|[Design an Array Statistics Tracker](./solutions/3369-design-an-array-statistics-tracker.js)|Hard| +3370|[Smallest Number With All Set Bits](./solutions/3370-smallest-number-with-all-set-bits.js)|Easy| 3372|[Maximize the Number of Target Nodes After Connecting Trees I](./solutions/3372-maximize-the-number-of-target-nodes-after-connecting-trees-i.js)|Medium| 3373|[Maximize the Number of Target Nodes After Connecting Trees II](./solutions/3373-maximize-the-number-of-target-nodes-after-connecting-trees-ii.js)|Hard| 3375|[Minimum Operations to Make Array Values Equal to K](./solutions/3375-minimum-operations-to-make-array-values-equal-to-k.js)|Easy| diff --git a/solutions/3370-smallest-number-with-all-set-bits.js b/solutions/3370-smallest-number-with-all-set-bits.js new file mode 100644 index 00000000..ea6b33ca --- /dev/null +++ b/solutions/3370-smallest-number-with-all-set-bits.js @@ -0,0 +1,20 @@ +/** + * 3370. Smallest Number With All Set Bits + * https://leetcode.com/problems/smallest-number-with-all-set-bits/ + * Difficulty: Easy + * + * You are given a positive number n. + * + * Return the smallest number x greater than or equal to n, such that the binary + * representation of x contains only set bits + */ + +/** + * @param {number} n + * @return {number} + */ +var smallestNumber = function(n) { + const bits = n.toString(2).length; + const result = (1 << bits) - 1; + return result >= n ? result : (1 << (bits + 1)) - 1; +}; From fb56d5f16362166c08fb0502376576cedf9edf4e Mon Sep 17 00:00:00 2001 From: Josh Crozier <5490537+JoshCrozier@users.noreply.github.com> Date: Thu, 30 Oct 2025 19:32:03 -0500 Subject: [PATCH 11/23] Add solution #3289 --- README.md | 1 + ...89-the-two-sneaky-numbers-of-digitville.js | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 solutions/3289-the-two-sneaky-numbers-of-digitville.js diff --git a/README.md b/README.md index 7260425c..e25cb275 100644 --- a/README.md +++ b/README.md @@ -2695,6 +2695,7 @@ 3272|[Find the Count of Good Integers](./solutions/3272-find-the-count-of-good-integers.js)|Hard| 3279|[Maximum Total Area Occupied by Pistons](./solutions/3279-maximum-total-area-occupied-by-pistons.js)|Hard| 3284|[Sum of Consecutive Subarrays](./solutions/3284-sum-of-consecutive-subarrays.js)|Medium| +3289|[The Two Sneaky Numbers of Digitville](./solutions/3289-the-two-sneaky-numbers-of-digitville.js)|Easy| 3294|[Convert Doubly Linked List to Array II](./solutions/3294-convert-doubly-linked-list-to-array-ii.js)|Medium| 3299|[Sum of Consecutive Subsequences](./solutions/3299-sum-of-consecutive-subsequences.js)|Hard| 3304|[Find the K-th Character in String Game I](./solutions/3304-find-the-k-th-character-in-string-game-i.js)|Easy| diff --git a/solutions/3289-the-two-sneaky-numbers-of-digitville.js b/solutions/3289-the-two-sneaky-numbers-of-digitville.js new file mode 100644 index 00000000..00f41559 --- /dev/null +++ b/solutions/3289-the-two-sneaky-numbers-of-digitville.js @@ -0,0 +1,31 @@ +/** + * 3289. The Two Sneaky Numbers of Digitville + * https://leetcode.com/problems/the-two-sneaky-numbers-of-digitville/ + * Difficulty: Easy + * + * In the town of Digitville, there was a list of numbers called nums containing integers + * from 0 to n - 1. Each number was supposed to appear exactly once in the list, however, + * two mischievous numbers sneaked in an additional time, making the list longer than usual. + * + * As the town detective, your task is to find these two sneaky numbers. Return an array of + * size two containing the two numbers (in any order), so peace can return to Digitville. + */ + +/** + * @param {number[]} nums + * @return {number[]} + */ +var getSneakyNumbers = function(nums) { + const set = new Set(); + const result = []; + + for (const n of nums) { + if (set.has(n)) { + result.push(n); + } else { + set.add(n); + } + } + + return result; +}; From e2e7a19bd9344471f4f0bc711bca758a36191132 Mon Sep 17 00:00:00 2001 From: Josh Crozier <5490537+JoshCrozier@users.noreply.github.com> Date: Fri, 31 Oct 2025 19:37:44 -0500 Subject: [PATCH 12/23] Add solution #3217 --- README.md | 1 + ...nodes-from-linked-list-present-in-array.js | 37 +++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 solutions/3217-delete-nodes-from-linked-list-present-in-array.js diff --git a/README.md b/README.md index e25cb275..a795cb17 100644 --- a/README.md +++ b/README.md @@ -2683,6 +2683,7 @@ 3205|[Maximum Array Hopping Score I](./solutions/3205-maximum-array-hopping-score-i.js)|Medium| 3208|[Alternating Groups II](./solutions/3208-alternating-groups-ii.js)|Medium| 3215|[Count Triplets with Even XOR Set Bits II](./solutions/3215-count-triplets-with-even-xor-set-bits-ii.js)|Medium| +3217|[Delete Nodes From Linked List Present in Array](./solutions/3217-delete-nodes-from-linked-list-present-in-array.js)|Medium| 3221|[Maximum Array Hopping Score II](./solutions/3221-maximum-array-hopping-score-ii.js)|Medium| 3223|[Minimum Length of String After Operations](./solutions/3223-minimum-length-of-string-after-operations.js)|Medium| 3227|[Vowels Game in a String](./solutions/3227-vowels-game-in-a-string.js)|Medium| diff --git a/solutions/3217-delete-nodes-from-linked-list-present-in-array.js b/solutions/3217-delete-nodes-from-linked-list-present-in-array.js new file mode 100644 index 00000000..62329285 --- /dev/null +++ b/solutions/3217-delete-nodes-from-linked-list-present-in-array.js @@ -0,0 +1,37 @@ +/** + * 3217. Delete Nodes From Linked List Present in Array + * https://leetcode.com/problems/delete-nodes-from-linked-list-present-in-array/ + * Difficulty: Medium + * + * You are given an array of integers nums and the head of a linked list. Return the head + * of the modified linked list after removing all nodes from the linked list that have + * a value that exists in nums. + */ + +/** + * Definition for singly-linked list. + * function ListNode(val, next) { + * this.val = (val===undefined ? 0 : val) + * this.next = (next===undefined ? null : next) + * } + */ +/** + * @param {number[]} nums + * @param {ListNode} head + * @return {ListNode} + */ +var modifiedList = function(nums, head) { + const set = new Set(nums); + const temp = new ListNode(0, head); + let current = temp; + + while (current.next) { + if (set.has(current.next.val)) { + current.next = current.next.next; + } else { + current = current.next; + } + } + + return temp.next; +}; From 1c392a98170db7ea158c1596554f7c3338ecbd69 Mon Sep 17 00:00:00 2001 From: Josh Crozier <5490537+JoshCrozier@users.noreply.github.com> Date: Mon, 3 Nov 2025 22:48:41 -0600 Subject: [PATCH 13/23] Add solution #3318 --- README.md | 1 + ...18-find-x-sum-of-all-k-long-subarrays-i.js | 60 +++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 solutions/3318-find-x-sum-of-all-k-long-subarrays-i.js diff --git a/README.md b/README.md index a795cb17..ca4318ab 100644 --- a/README.md +++ b/README.md @@ -2703,6 +2703,7 @@ 3306|[Count of Substrings Containing Every Vowel and K Consonants II](./solutions/3306-count-of-substrings-containing-every-vowel-and-k-consonants-ii.js)|Medium| 3307|[Find the K-th Character in String Game II](./solutions/3307-find-the-k-th-character-in-string-game-ii.js)|Hard| 3313|[Find the Last Marked Nodes in Tree](./solutions/3313-find-the-last-marked-nodes-in-tree.js)|Hard| +3318|[Find X-Sum of All K-Long Subarrays I](./solutions/3318-find-x-sum-of-all-k-long-subarrays-i.js)|Easy| 3323|[Minimize Connected Groups by Inserting Interval](./solutions/3323-minimize-connected-groups-by-inserting-interval.js)|Medium| 3329|[Count Substrings With K-Frequency Characters II](./solutions/3329-count-substrings-with-k-frequency-characters-ii.js)|Hard| 3330|[Find the Original Typed String I](./solutions/3330-find-the-original-typed-string-i.js)|Easy| diff --git a/solutions/3318-find-x-sum-of-all-k-long-subarrays-i.js b/solutions/3318-find-x-sum-of-all-k-long-subarrays-i.js new file mode 100644 index 00000000..2a030036 --- /dev/null +++ b/solutions/3318-find-x-sum-of-all-k-long-subarrays-i.js @@ -0,0 +1,60 @@ +/** + * 3318. Find X-Sum of All K-Long Subarrays I + * https://leetcode.com/problems/find-x-sum-of-all-k-long-subarrays-i/ + * Difficulty: Easy + * + * You are given an array nums of n integers and two integers k and x. + * + * The x-sum of an array is calculated by the following procedure: + * - Count the occurrences of all elements in the array. + * - Keep only the occurrences of the top x most frequent elements. If two elements + * have the same number of occurrences, the element with the bigger value is + * considered more frequent. + * - Calculate the sum of the resulting array. + * + * Note that if an array has less than x distinct elements, its x-sum is the sum of the array. + * + Return an integer array answer of length n - k + 1 where answer[i] is the x-sum of the + subarray nums[i..i + k - 1]. + */ + +/** + * @param {number[]} nums + * @param {number} k + * @param {number} x + * @return {number[]} + */ +var findXSum = function(nums, k, x) { + const n = nums.length; + const result = []; + + for (let i = 0; i <= n - k; i++) { + const subarray = nums.slice(i, i + k); + result.push(helper(subarray)); + } + + return result; + + function helper(subarray) { + const frequency = new Map(); + + for (const num of subarray) { + frequency.set(num, (frequency.get(num) || 0) + 1); + } + + const elements = Array.from(frequency.entries()); + elements.sort((a, b) => { + if (a[1] !== b[1]) return b[1] - a[1]; + return b[0] - a[0]; + }); + + const topX = elements.slice(0, x); + let sum = 0; + + for (const [value, count] of topX) { + sum += value * count; + } + + return sum; + } +}; From 4261a9d1b0a0827a2ec3dab68f79b388efa95f10 Mon Sep 17 00:00:00 2001 From: Josh Crozier <5490537+JoshCrozier@users.noreply.github.com> Date: Fri, 21 Nov 2025 23:02:32 -0600 Subject: [PATCH 14/23] Add solution #3190 --- README.md | 1 + ...to-make-all-elements-divisible-by-three.js | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 solutions/3190-find-minimum-operations-to-make-all-elements-divisible-by-three.js diff --git a/README.md b/README.md index ca4318ab..84a04ff2 100644 --- a/README.md +++ b/README.md @@ -2674,6 +2674,7 @@ 3183|[The Number of Ways to Make the Sum](./solutions/3183-the-number-of-ways-to-make-the-sum.js)|Medium| 3186|[Maximum Total Damage With Spell Casting](./solutions/3186-maximum-total-damage-with-spell-casting.js)|Medium| 3189|[Minimum Moves to Get a Peaceful Board](./solutions/3189-minimum-moves-to-get-a-peaceful-board.js)|Medium| +3190|[Find Minimum Operations to Make All Elements Divisible by Three](./solutions/3190-find-minimum-operations-to-make-all-elements-divisible-by-three.js)|Easy| 3191|[Minimum Operations to Make Binary Array Elements Equal to One I](./solutions/3191-minimum-operations-to-make-binary-array-elements-equal-to-one-i.js)|Medium| 3195|[Find the Minimum Area to Cover All Ones I](./solutions/3195-find-the-minimum-area-to-cover-all-ones-i.js)|Medium| 3197|[Find the Minimum Area to Cover All Ones II](./solutions/3197-find-the-minimum-area-to-cover-all-ones-ii.js)|Hard| diff --git a/solutions/3190-find-minimum-operations-to-make-all-elements-divisible-by-three.js b/solutions/3190-find-minimum-operations-to-make-all-elements-divisible-by-three.js new file mode 100644 index 00000000..24effff2 --- /dev/null +++ b/solutions/3190-find-minimum-operations-to-make-all-elements-divisible-by-three.js @@ -0,0 +1,21 @@ +/** + * 3190. Find Minimum Operations to Make All Elements Divisible by Three + * https://leetcode.com/problems/find-minimum-operations-to-make-all-elements-divisible-by-three/ + * Difficulty: Easy + * + * You are given an integer array nums. In one operation, you can add or subtract 1 from any + * element of nums. + * + * Return the minimum number of operations to make all elements of nums divisible by 3. + */ + +/** + * @param {number[]} nums + * @return {number} + */ +var minimumOperations = function(nums) { + return nums.reduce((operations, num) => { + const remainder = num % 3; + return operations + Math.min(remainder, 3 - remainder); + }, 0); +}; From 0fa289360baf48d556f552c98b95eba22eaef14c Mon Sep 17 00:00:00 2001 From: Josh Crozier <5490537+JoshCrozier@users.noreply.github.com> Date: Fri, 28 Nov 2025 20:08:47 -0600 Subject: [PATCH 15/23] Add solution #3512 --- README.md | 1 + ...ations-to-make-array-sum-divisible-by-k.js | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 solutions/3512-minimum-operations-to-make-array-sum-divisible-by-k.js diff --git a/README.md b/README.md index 84a04ff2..baec6d02 100644 --- a/README.md +++ b/README.md @@ -2778,6 +2778,7 @@ 3506|[Find Time Required to Eliminate Bacterial Strains](./solutions/3506-find-time-required-to-eliminate-bacterial-strains.js)|Hard| 3508|[Implement Router](./solutions/3508-implement-router.js)|Medium| 3511|[Make a Positive Array](./solutions/3511-make-a-positive-array.js)|Medium| +3512|[Minimum Operations to Make Array Sum Divisible by K](./solutions/3512-minimum-operations-to-make-array-sum-divisible-by-k.js)|Easy| 3516|[Find Closest Person](./solutions/3516-find-closest-person.js)|Easy| 3520|[Minimum Threshold for Inversion Pairs Count](./solutions/3520-minimum-threshold-for-inversion-pairs-count.js)|Medium| 3535|[Unit Conversion II](./solutions/3535-unit-conversion-ii.js)|Medium| diff --git a/solutions/3512-minimum-operations-to-make-array-sum-divisible-by-k.js b/solutions/3512-minimum-operations-to-make-array-sum-divisible-by-k.js new file mode 100644 index 00000000..772bb4dc --- /dev/null +++ b/solutions/3512-minimum-operations-to-make-array-sum-divisible-by-k.js @@ -0,0 +1,21 @@ +/** + * 3512. Minimum Operations to Make Array Sum Divisible by K + * https://leetcode.com/problems/minimum-operations-to-make-array-sum-divisible-by-k/ + * Difficulty: Easy + * + * You are given an integer array nums and an integer k. You can perform the following + * operation any number of times: + * - Select an index i and replace nums[i] with nums[i] - 1. + * + * Return the minimum number of operations required to make the sum of the array + * divisible by k. + */ + +/** + * @param {number[]} nums + * @param {number} k + * @return {number} + */ +var minOperations = function(nums, k) { + return nums.reduce((sum, n) => sum + n, 0) % k; +}; From 525affe065685b8d9b88bc7e1c27437ebe59e41b Mon Sep 17 00:00:00 2001 From: Josh Crozier <5490537+JoshCrozier@users.noreply.github.com> Date: Tue, 2 Dec 2025 09:30:49 -0600 Subject: [PATCH 16/23] Add solution #3623 --- README.md | 1 + .../3623-count-number-of-trapezoids-i.js | 40 +++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 solutions/3623-count-number-of-trapezoids-i.js diff --git a/README.md b/README.md index baec6d02..462df0a8 100644 --- a/README.md +++ b/README.md @@ -2784,6 +2784,7 @@ 3535|[Unit Conversion II](./solutions/3535-unit-conversion-ii.js)|Medium| 3539|[Find Sum of Array Product of Magical Sequences](./solutions/3539-find-sum-of-array-product-of-magical-sequences.js)|Hard| 3541|[Find Most Frequent Vowel and Consonant](./solutions/3541-find-most-frequent-vowel-and-consonant.js)|Easy| +3623|[Count Number of Trapezoids I](./solutions/3623-count-number-of-trapezoids-i.js)|Medium| ## License diff --git a/solutions/3623-count-number-of-trapezoids-i.js b/solutions/3623-count-number-of-trapezoids-i.js new file mode 100644 index 00000000..6bf59051 --- /dev/null +++ b/solutions/3623-count-number-of-trapezoids-i.js @@ -0,0 +1,40 @@ +/** + * 3623. Count Number of Trapezoids I + * https://leetcode.com/problems/count-number-of-trapezoids-i/ + * Difficulty: Medium + * + * You are given a 2D integer array points, where points[i] = [xi, yi] represents the + * coordinates of the ith point on the Cartesian plane. + * + * A horizontal trapezoid is a convex quadrilateral with at least one pair of horizontal + * sides (i.e. parallel to the x-axis). Two lines are parallel if and only if they have + * the same slope. + * + * Return the number of unique horizontal trapezoids that can be formed by choosing any + * four distinct points from points. + * + * Since the answer may be very large, return it modulo 109 + 7. + */ + +/** + * @param {number[][]} points + * @return {number} + */ +var countTrapezoids = function(points) { + const MOD = 1e9 + 7; + + const map = new Map(); + for (const [x, y] of points) { + map.set(y, (map.get(y) || 0) + 1); + } + + let result = 0n; + let total = 0n; + for (const count of map.values()) { + const lines = BigInt(count) * BigInt(count - 1) / 2n; + result = (result + total * lines) % BigInt(MOD); + total = (total + lines) % BigInt(MOD); + } + + return Number(result); +}; From 8eee07f5ab39bc42011990c20836a319b77f5ba2 Mon Sep 17 00:00:00 2001 From: Josh Crozier <5490537+JoshCrozier@users.noreply.github.com> Date: Thu, 4 Dec 2025 19:24:57 -0600 Subject: [PATCH 17/23] Add solution #3432 --- README.md | 1 + ...unt-partitions-with-even-sum-difference.js | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 solutions/3432-count-partitions-with-even-sum-difference.js diff --git a/README.md b/README.md index 462df0a8..b7145b85 100644 --- a/README.md +++ b/README.md @@ -2749,6 +2749,7 @@ 3422|[Minimum Operations to Make Subarray Elements Equal](./solutions/3422-minimum-operations-to-make-subarray-elements-equal.js)|Medium| 3423|[Maximum Difference Between Adjacent Elements in a Circular Array](./solutions/3423-maximum-difference-between-adjacent-elements-in-a-circular-array.js)|Easy| 3431|[Minimum Unlocked Indices to Sort Nums](./solutions/3431-minimum-unlocked-indices-to-sort-nums.js)|Medium| +3432|[Count Partitions with Even Sum Difference](./solutions/3432-count-partitions-with-even-sum-difference.js)|Easy| 3437|[Permutations III](./solutions/3437-permutations-iii.js)|Medium| 3439|[Reschedule Meetings for Maximum Free Time I](./solutions/3439-reschedule-meetings-for-maximum-free-time-i.js)|Medium| 3440|[Reschedule Meetings for Maximum Free Time II](./solutions/3440-reschedule-meetings-for-maximum-free-time-ii.js)|Medium| diff --git a/solutions/3432-count-partitions-with-even-sum-difference.js b/solutions/3432-count-partitions-with-even-sum-difference.js new file mode 100644 index 00000000..a1252690 --- /dev/null +++ b/solutions/3432-count-partitions-with-even-sum-difference.js @@ -0,0 +1,31 @@ +/** + * 3432. Count Partitions with Even Sum Difference + * https://leetcode.com/problems/count-partitions-with-even-sum-difference/ + * Difficulty: Easy + * + * You are given an integer array nums of length n. + * + * A partition is defined as an index i where 0 <= i < n - 1, splitting the array into two + * non-empty subarrays such that: + * - Left subarray contains indices [0, i]. + * - Right subarray contains indices [i + 1, n - 1]. + * + * Return the number of partitions where the difference between the sum of the left and right + * subarrays is even. + */ + +/** + * @param {number[]} nums + * @return {number} + */ +var countPartitions = function(nums) { + const total = nums.reduce((sum, n) => sum + n, 0); + let result = 0; + + for (let i = 0, left = 0; i < nums.length - 1; i++) { + left += nums[i]; + result += (left - total - left) % 2 === 0 ? 1 : 0; + } + + return result; +}; From dc674c30f65757d91819a68e962cf800ee8084c3 Mon Sep 17 00:00:00 2001 From: Josh Crozier <5490537+JoshCrozier@users.noreply.github.com> Date: Sat, 6 Dec 2025 00:48:22 -0600 Subject: [PATCH 18/23] Add solution #3578 --- README.md | 1 + ...tions-with-max-min-difference-at-most-k.js | 48 +++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 solutions/3578-count-partitions-with-max-min-difference-at-most-k.js diff --git a/README.md b/README.md index b7145b85..c4903bd0 100644 --- a/README.md +++ b/README.md @@ -2785,6 +2785,7 @@ 3535|[Unit Conversion II](./solutions/3535-unit-conversion-ii.js)|Medium| 3539|[Find Sum of Array Product of Magical Sequences](./solutions/3539-find-sum-of-array-product-of-magical-sequences.js)|Hard| 3541|[Find Most Frequent Vowel and Consonant](./solutions/3541-find-most-frequent-vowel-and-consonant.js)|Easy| +3578|[Count Partitions With Max-Min Difference at Most K](./solutions/3578-count-partitions-with-max-min-difference-at-most-k.js)|Medium| 3623|[Count Number of Trapezoids I](./solutions/3623-count-number-of-trapezoids-i.js)|Medium| ## License diff --git a/solutions/3578-count-partitions-with-max-min-difference-at-most-k.js b/solutions/3578-count-partitions-with-max-min-difference-at-most-k.js new file mode 100644 index 00000000..670677b2 --- /dev/null +++ b/solutions/3578-count-partitions-with-max-min-difference-at-most-k.js @@ -0,0 +1,48 @@ +/** + * 3578. Count Partitions With Max-Min Difference at Most K + * https://leetcode.com/problems/count-partitions-with-max-min-difference-at-most-k + * Difficulty: Medium + * + * You are given an integer array nums and an integer k. Your task is to partition nums + * into one or more non-empty contiguous segments such that in each segment, the difference + * between its maximum and minimum elements is at most k. + * + * Return the total number of ways to partition nums under this condition. + * + * Since the answer may be too large, return it modulo 109 + 7. + */ + +/** + * @param {number[]} nums + * @param {number} k + * @return {number} + */ +var countPartitions = function(nums, k) { + const MOD = 1e9 + 7; + const n = nums.length; + const dp = new Array(n + 1).fill(0); + dp[0] = 1; + + let sum = 1; + const minQueue = []; + const maxQueue = []; + + for (let left = 0, right = 0; right < n; right++) { + while (maxQueue.length && nums[right] > nums[maxQueue.at(-1)]) maxQueue.pop(); + maxQueue.push(right); + + while (minQueue.length && nums[right] < nums[minQueue.at(-1)]) minQueue.pop(); + minQueue.push(right); + + while (nums[maxQueue[0]] - nums[minQueue[0]] > k) { + sum = (sum - dp[left++] + MOD) % MOD; + if (minQueue[0] < left) minQueue.shift(); + if (maxQueue[0] < left) maxQueue.shift(); + } + + dp[right + 1] = sum; + sum = (sum + dp[right + 1]) % MOD; + } + + return dp[n]; +}; From 143fcd7c19541ea6ccbc8ba37844ac76d58d7f4f Mon Sep 17 00:00:00 2001 From: Josh Crozier <5490537+JoshCrozier@users.noreply.github.com> Date: Tue, 9 Dec 2025 16:54:36 -0600 Subject: [PATCH 19/23] Add solution #3583 --- README.md | 1 + solutions/3583-count-special-triplets.js | 48 ++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 solutions/3583-count-special-triplets.js diff --git a/README.md b/README.md index c4903bd0..f64ec12c 100644 --- a/README.md +++ b/README.md @@ -2786,6 +2786,7 @@ 3539|[Find Sum of Array Product of Magical Sequences](./solutions/3539-find-sum-of-array-product-of-magical-sequences.js)|Hard| 3541|[Find Most Frequent Vowel and Consonant](./solutions/3541-find-most-frequent-vowel-and-consonant.js)|Easy| 3578|[Count Partitions With Max-Min Difference at Most K](./solutions/3578-count-partitions-with-max-min-difference-at-most-k.js)|Medium| +3583|[Count Special Triplets](./solutions/3583-count-special-triplets.js)|Medium| 3623|[Count Number of Trapezoids I](./solutions/3623-count-number-of-trapezoids-i.js)|Medium| ## License diff --git a/solutions/3583-count-special-triplets.js b/solutions/3583-count-special-triplets.js new file mode 100644 index 00000000..963a4633 --- /dev/null +++ b/solutions/3583-count-special-triplets.js @@ -0,0 +1,48 @@ +/** + * 3583. Count Special Triplets + * https://leetcode.com/problems/count-special-triplets/ + * Difficulty: Medium + * + * You are given an integer array nums. + * + * A special triplet is defined as a triplet of indices (i, j, k) such that: + * - 0 <= i < j < k < n, where n = nums.length + * - nums[i] == nums[j] * 2 + * - nums[k] == nums[j] * 2 + * + * Return the total number of special triplets in the array. + * + * Since the answer may be large, return it modulo 109 + 7. + */ + +/** + * @param {number[]} nums + * @return {number} + */ +var specialTriplets = function(nums) { + const MOD = 1e9 + 7; + let result = 0; + + const leftCount = new Map(); + const rightCount = new Map(); + for (const num of nums) { + rightCount.set(num, (rightCount.get(num) || 0) + 1); + } + + for (let j = 0; j < nums.length; j++) { + const middle = nums[j]; + const target = middle * 2; + + rightCount.set(middle, rightCount.get(middle) - 1); + if (rightCount.get(middle) === 0) { + rightCount.delete(middle); + } + + const left = leftCount.get(target) || 0; + const right = rightCount.get(target) || 0; + result = (result + left * right) % MOD; + leftCount.set(middle, (leftCount.get(middle) || 0) + 1); + } + + return result; +}; From afdbf805f6a603ee9099668666186ff09127d81b Mon Sep 17 00:00:00 2001 From: Josh Crozier <5490537+JoshCrozier@users.noreply.github.com> Date: Wed, 10 Dec 2025 16:12:41 -0600 Subject: [PATCH 20/23] Add solution #3577 --- README.md | 1 + ...mber-of-computer-unlocking-permutations.js | 50 +++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 solutions/3577-count-the-number-of-computer-unlocking-permutations.js diff --git a/README.md b/README.md index f64ec12c..8b9e0c89 100644 --- a/README.md +++ b/README.md @@ -2785,6 +2785,7 @@ 3535|[Unit Conversion II](./solutions/3535-unit-conversion-ii.js)|Medium| 3539|[Find Sum of Array Product of Magical Sequences](./solutions/3539-find-sum-of-array-product-of-magical-sequences.js)|Hard| 3541|[Find Most Frequent Vowel and Consonant](./solutions/3541-find-most-frequent-vowel-and-consonant.js)|Easy| +3577|[Count the Number of Computer Unlocking Permutations](./solutions/3577-count-the-number-of-computer-unlocking-permutations.js)|Medium| 3578|[Count Partitions With Max-Min Difference at Most K](./solutions/3578-count-partitions-with-max-min-difference-at-most-k.js)|Medium| 3583|[Count Special Triplets](./solutions/3583-count-special-triplets.js)|Medium| 3623|[Count Number of Trapezoids I](./solutions/3623-count-number-of-trapezoids-i.js)|Medium| diff --git a/solutions/3577-count-the-number-of-computer-unlocking-permutations.js b/solutions/3577-count-the-number-of-computer-unlocking-permutations.js new file mode 100644 index 00000000..6accf0de --- /dev/null +++ b/solutions/3577-count-the-number-of-computer-unlocking-permutations.js @@ -0,0 +1,50 @@ +/** + * 3577. Count the Number of Computer Unlocking Permutations + * https://leetcode.com/problems/count-the-number-of-computer-unlocking-permutations/ + * Difficulty: Medium + * + * You are given an array complexity of length n. + * + * There are n locked computers in a room with labels from 0 to n - 1, each with its own + * unique password. The password of the computer i has a complexity complexity[i]. + * + * The password for the computer labeled 0 is already decrypted and serves as the root. + * All other computers must be unlocked using it or another previously unlocked computer, + * following this information: + * - You can decrypt the password for the computer i using the password for computer j, + * where j is any integer less than i with a lower complexity. (i.e. j < i and + * complexity[j] < complexity[i]) + * - To decrypt the password for computer i, you must have already unlocked a computer j + * such that j < i and complexity[j] < complexity[i]. + * + * Find the number of permutations of [0, 1, 2, ..., (n - 1)] that represent a valid order + * in which the computers can be unlocked, starting from computer 0 as the only initially + * unlocked one. + * + * Since the answer may be large, return it modulo 109 + 7. + * + * Note that the password for the computer with label 0 is decrypted, and not the computer + * with the first position in the permutation. + */ + +/** + * @param {number[]} complexity + * @return {number} + */ +var countPermutations = function(complexity) { + const MOD = 1e9 + 7; + const root = complexity[0]; + + for (let i = 1; i < complexity.length; i++) { + if (complexity[i] <= root) { + return 0; + } + } + + let result = 1; + for (let i = 2; i < complexity.length; i++) { + result = (result * i) % MOD; + } + + return result; +}; From 2701caf365f44480e4d1252463e75c5b47aeb3ca Mon Sep 17 00:00:00 2001 From: Josh Crozier <5490537+JoshCrozier@users.noreply.github.com> Date: Thu, 11 Dec 2025 20:56:48 -0600 Subject: [PATCH 21/23] Add solution #3531 --- README.md | 1 + solutions/3531-count-covered-buildings.js | 52 +++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 solutions/3531-count-covered-buildings.js diff --git a/README.md b/README.md index 8b9e0c89..75bb6768 100644 --- a/README.md +++ b/README.md @@ -2782,6 +2782,7 @@ 3512|[Minimum Operations to Make Array Sum Divisible by K](./solutions/3512-minimum-operations-to-make-array-sum-divisible-by-k.js)|Easy| 3516|[Find Closest Person](./solutions/3516-find-closest-person.js)|Easy| 3520|[Minimum Threshold for Inversion Pairs Count](./solutions/3520-minimum-threshold-for-inversion-pairs-count.js)|Medium| +3531|[Count Covered Buildings](./solutions/3531-count-covered-buildings.js)|Medium| 3535|[Unit Conversion II](./solutions/3535-unit-conversion-ii.js)|Medium| 3539|[Find Sum of Array Product of Magical Sequences](./solutions/3539-find-sum-of-array-product-of-magical-sequences.js)|Hard| 3541|[Find Most Frequent Vowel and Consonant](./solutions/3541-find-most-frequent-vowel-and-consonant.js)|Easy| diff --git a/solutions/3531-count-covered-buildings.js b/solutions/3531-count-covered-buildings.js new file mode 100644 index 00000000..ea6dc148 --- /dev/null +++ b/solutions/3531-count-covered-buildings.js @@ -0,0 +1,52 @@ +/** + * 3531. Count Covered Buildings + * https://leetcode.com/problems/count-covered-buildings/ + * Difficulty: Medium + * + * You are given a positive integer n, representing an n x n city. You are also given + * a 2D grid buildings, where buildings[i] = [x, y] denotes a unique building located + * at coordinates [x, y]. + * + * A building is covered if there is at least one building in all four directions: + * left, right, above, and below. + * + * Return the number of covered buildings. + */ + +/** + * @param {number} n + * @param {number[][]} buildings + * @return {number} + */ +var countCoveredBuildings = function(n, buildings) { + const rowBuildings = new Map(); + const colBuildings = new Map(); + let result = 0; + + for (const [x, y] of buildings) { + if (!rowBuildings.has(x)) rowBuildings.set(x, []); + if (!colBuildings.has(y)) colBuildings.set(y, []); + rowBuildings.get(x).push(y); + colBuildings.get(y).push(x); + } + for (const coords of rowBuildings.values()) { + coords.sort((a, b) => a - b); + } + for (const coords of colBuildings.values()) { + coords.sort((a, b) => a - b); + } + for (const [x, y] of buildings) { + const rowCoords = rowBuildings.get(x); + const colCoords = colBuildings.get(y); + const hasLeft = rowCoords[0] < y; + const hasRight = rowCoords[rowCoords.length - 1] > y; + const hasAbove = colCoords[0] < x; + const hasBelow = colCoords[colCoords.length - 1] > x; + + if (hasLeft && hasRight && hasAbove && hasBelow) { + result++; + } + } + + return result; +}; From 09354effd9b8212746218f2ac37dd3671acffb47 Mon Sep 17 00:00:00 2001 From: Josh Crozier <5490537+JoshCrozier@users.noreply.github.com> Date: Thu, 11 Dec 2025 21:02:13 -0600 Subject: [PATCH 22/23] Add solution #3433 --- README.md | 1 + solutions/3433-count-mentions-per-user.js | 74 +++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 solutions/3433-count-mentions-per-user.js diff --git a/README.md b/README.md index 75bb6768..b74dd36f 100644 --- a/README.md +++ b/README.md @@ -2750,6 +2750,7 @@ 3423|[Maximum Difference Between Adjacent Elements in a Circular Array](./solutions/3423-maximum-difference-between-adjacent-elements-in-a-circular-array.js)|Easy| 3431|[Minimum Unlocked Indices to Sort Nums](./solutions/3431-minimum-unlocked-indices-to-sort-nums.js)|Medium| 3432|[Count Partitions with Even Sum Difference](./solutions/3432-count-partitions-with-even-sum-difference.js)|Easy| +3433|[Count Mentions Per User](./solutions/3433-count-mentions-per-user.js)|Medium| 3437|[Permutations III](./solutions/3437-permutations-iii.js)|Medium| 3439|[Reschedule Meetings for Maximum Free Time I](./solutions/3439-reschedule-meetings-for-maximum-free-time-i.js)|Medium| 3440|[Reschedule Meetings for Maximum Free Time II](./solutions/3440-reschedule-meetings-for-maximum-free-time-ii.js)|Medium| diff --git a/solutions/3433-count-mentions-per-user.js b/solutions/3433-count-mentions-per-user.js new file mode 100644 index 00000000..0884460f --- /dev/null +++ b/solutions/3433-count-mentions-per-user.js @@ -0,0 +1,74 @@ +/** + * 3433. Count Mentions Per User + * https://leetcode.com/problems/count-mentions-per-user/ + * Difficulty: Medium + * + * You are given an integer numberOfUsers representing the total number of users and an + * array events of size n x 3. + * + * Each events[i] can be either of the following two types: + * 1. Message Event: ["MESSAGE", "timestampi", "mentions_stringi"] + * - This event indicates that a set of users was mentioned in a message at timestampi. + * - The mentions_stringi string can contain one of the following tokens: + * - id: where is an integer in range [0,numberOfUsers - 1]. There can + * be multiple ids separated by a single whitespace and may contain duplicates. This + * can mention even the offline users. + * - ALL: mentions all users. + * - HERE: mentions all online users. + * 2. Offline Event: ["OFFLINE", "timestampi", "idi"] + * - This event indicates that the user idi had become offline at timestampi for 60 time units. + * The user will automatically be online again at time timestampi + 60. + * + * Return an array mentions where mentions[i] represents the number of mentions the user with + * id i has across all MESSAGE events. + * + * All users are initially online, and if a user goes offline or comes back online, their status + * change is processed before handling any message event that occurs at the same timestamp. + * + * Note that a user can be mentioned multiple times in a single message event, and each mention + * should be counted separately. + */ + +/** + * @param {number} numberOfUsers + * @param {string[][]} events + * @return {number[]} + */ +var countMentions = function(numberOfUsers, events) { + const result = new Array(numberOfUsers).fill(0); + const onlineAt = new Array(numberOfUsers).fill(0); + + events.sort((a, b) => { + const timeCompare = parseInt(a[1], 10) - parseInt(b[1], 10); + if (timeCompare !== 0) return timeCompare; + return a[0] === 'MESSAGE' ? 1 : -1; + }); + + for (const [type, timestampStr, data] of events) { + const timestamp = parseInt(timestampStr, 10); + + if (type === 'OFFLINE') { + const userId = parseInt(data, 10); + onlineAt[userId] = timestamp + 60; + } else { + if (data === 'ALL') { + for (let i = 0; i < numberOfUsers; i++) { + result[i]++; + } + } else if (data === 'HERE') { + for (let i = 0; i < numberOfUsers; i++) { + if (onlineAt[i] <= timestamp) { + result[i]++; + } + } + } else { + const ids = data.replace(/id/g, '').split(' '); + for (const id of ids) { + result[parseInt(id, 10)]++; + } + } + } + } + + return result; +}; From 399017ccbed8f4c7c1b10cb173336f670c8c25e1 Mon Sep 17 00:00:00 2001 From: Josh Crozier <5490537+JoshCrozier@users.noreply.github.com> Date: Fri, 12 Dec 2025 19:05:47 -0600 Subject: [PATCH 23/23] Add solution #3606 --- README.md | 1 + solutions/3606-coupon-code-validator.js | 43 +++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 solutions/3606-coupon-code-validator.js diff --git a/README.md b/README.md index b74dd36f..f66df85b 100644 --- a/README.md +++ b/README.md @@ -2790,6 +2790,7 @@ 3577|[Count the Number of Computer Unlocking Permutations](./solutions/3577-count-the-number-of-computer-unlocking-permutations.js)|Medium| 3578|[Count Partitions With Max-Min Difference at Most K](./solutions/3578-count-partitions-with-max-min-difference-at-most-k.js)|Medium| 3583|[Count Special Triplets](./solutions/3583-count-special-triplets.js)|Medium| +3606|[Coupon Code Validator](./solutions/3606-coupon-code-validator.js)|Easy| 3623|[Count Number of Trapezoids I](./solutions/3623-count-number-of-trapezoids-i.js)|Medium| ## License diff --git a/solutions/3606-coupon-code-validator.js b/solutions/3606-coupon-code-validator.js new file mode 100644 index 00000000..356e22c4 --- /dev/null +++ b/solutions/3606-coupon-code-validator.js @@ -0,0 +1,43 @@ +/** + * 3606. Coupon Code Validator + * https://leetcode.com/problems/coupon-code-validator/ + * Difficulty: Easy + * + * You are given three arrays of length n that describe the properties of n + * coupons: code, businessLine, and isActive. The ith coupon has: + * - code[i]: a string representing the coupon identifier. + * - businessLine[i]: a string denoting the business category of the coupon. + * - isActive[i]: a boolean indicating whether the coupon is currently active. + * + * A coupon is considered valid if all of the following conditions hold: + * 1. code[i] is non-empty and consists only of alphanumeric characters (a-z, A-Z, 0-9) + * and underscores (_). + * 2. businessLine[i] is one of the following four categories: "electronics", "grocery", + * "pharmacy", "restaurant". + * 3. isActive[i] is true. + * + * Return an array of the codes of all valid coupons, sorted first by their businessLine + * in the order: "electronics", "grocery", "pharmacy", "restaurant", and then by code in + * lexicographical (ascending) order within each category. + */ + +/** + * @param {string[]} code + * @param {string[]} businessLine + * @param {boolean[]} isActive + * @return {string[]} + */ +var validateCoupons = function(code, businessLine, isActive) { + const whitelist = ['electronics', 'grocery', 'pharmacy', 'restaurant']; + const order = { electronics: 0, grocery: 1, pharmacy: 2, restaurant: 3 }; + + return code + .map((id, i) => ({ id, name: businessLine[i], active: isActive[i] })) + .filter(({ id, name, active }) => active && whitelist.includes(name) && /^[\da-z_]+$/i.test(id)) + .sort((a, b) => { + const compare = order[a.name] - order[b.name]; + if (compare !== 0) return compare; + return a.id < b.id ? -1 : a.id > b.id ? 1 : 0; + }) + .map(({ id }) => id); +};