diff --git a/README.md b/README.md index a795cb17..baec6d02 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| @@ -2703,6 +2704,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| @@ -2776,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/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); +}; 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; + } +}; 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; +};