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 01/15] 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 02/15] 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 03/15] 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 04/15] 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 05/15] 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 06/15] 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 07/15] 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 08/15] 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 09/15] 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 10/15] 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 11/15] 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 12/15] 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 13/15] 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 14/15] 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 15/15] 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); +};