Smallest Missing Non-negative Integer After Operations - Count Mod Values [JS]

Description 

Solution: Count Mod Values

Numbers will be grouped by their mod value (nums[i] % value).
Numbers in the same mod group can be transformed into any number with the same mod value.
Use a hashmap to count the occurances of numbers for each mod value.

Try to create each number from 0 to n - 1.
If there are no more occurances of numbers for the current mod value, then it is impossible to have a higher MEX.

Time Complexity: O(n)
Space Complexity: O(n)

var findSmallestInteger = function(nums, value) {
  let modCount = new Map();
  for (let num of nums) {
    let mod = ((num % value) + value) % value;
    modCount.set(mod, (modCount.get(mod) || 0) + 1);
  }
  for (let i = 0; i < nums.length; i++) {
    let modValue = i % value;
    if (modCount.has(modValue) && modCount.get(modValue) > 0) {
      modCount.set(modValue, modCount.get(modValue) - 1);
    } else {
      return i;
    }
  }
  return nums.length;
};

Comments

Popular posts from this blog

Maximum Value of an Ordered Triplet II - Two Solutions [JS]

Maximum Sum of Distinct Subarrays With Length K - Sliding Window w/ Two Pointers & Set [JS]

Sum of Prefix Scores of Strings - Trie [JS]

Maximum Count of Positive Integer and Negative Integer - Binary Search [JS]

Count Subarrays With Median K - Count Left & Right Balance [JS]