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/14] 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/14] 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/14] 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/14] 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/14] 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/14] 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/14] 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/14] 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/14] 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/14] 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/14] 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/14] 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/14] 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/14] 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); +};