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