|
| 1 | +/** |
| 2 | + * 2598. Smallest Missing Non-negative Integer After Operations |
| 3 | + * https://leetcode.com/problems/smallest-missing-non-negative-integer-after-operations/ |
| 4 | + * Difficulty: Medium |
| 5 | + * |
| 6 | + * You are given a 0-indexed integer array nums and an integer value. |
| 7 | + * |
| 8 | + * In one operation, you can add or subtract value from any element of nums. |
| 9 | + * - For example, if nums = [1,2,3] and value = 2, you can choose to subtract value from nums[0] |
| 10 | + * to make nums = [-1,2,3]. |
| 11 | + * |
| 12 | + * The MEX (minimum excluded) of an array is the smallest missing non-negative integer in it. |
| 13 | + * - For example, the MEX of [-1,2,3] is 0 while the MEX of [1,0,3] is 2. |
| 14 | + * |
| 15 | + * Return the maximum MEX of nums after applying the mentioned operation any number of times. |
| 16 | + */ |
| 17 | + |
| 18 | +/** |
| 19 | + * @param {number[]} nums |
| 20 | + * @param {number} value |
| 21 | + * @return {number} |
| 22 | + */ |
| 23 | +var findSmallestInteger = function(nums, value) { |
| 24 | + const remainderCount = new Map(); |
| 25 | + |
| 26 | + for (const num of nums) { |
| 27 | + const remainder = ((num % value) + value) % value; |
| 28 | + remainderCount.set(remainder, (remainderCount.get(remainder) || 0) + 1); |
| 29 | + } |
| 30 | + |
| 31 | + for (let mex = 0; mex <= nums.length; mex++) { |
| 32 | + const remainder = mex % value; |
| 33 | + if (!remainderCount.has(remainder) || remainderCount.get(remainder) === 0) { |
| 34 | + return mex; |
| 35 | + } |
| 36 | + remainderCount.set(remainder, remainderCount.get(remainder) - 1); |
| 37 | + } |
| 38 | + |
| 39 | + return nums.length; |
| 40 | +}; |
0 commit comments