Problem solution · Python

Lexicographically Maximum Mex Array

Lexicographically Maximum Mex Array: a Python solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to Kamyu LeetCode Solutions.

Technique
Direct simulation
Source
Kamyu LeetCode Solutions
Length
88 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Direct simulation

For Lexicographically Maximum Mex Array, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

  • 88 lines of Python from the credited upstream file lexicographically-maximum-mex-array.py.
  • The implementation visibly relies on sequence storage.
  • No explicit loop blocks detected.

Complexity

Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from Kamyu LeetCode Solutions by kamyu104 and is used under the MIT licence.

Full codeLexicographically Maximum Mex Array · PythonPython
Use this to learn the idea, then write your own version.
# Time:  O(n)# Space: O(n) # hash table, prefix sum, greedyclass Solution(object):    def maximumMEX(self, nums):        """        :type nums: List[int]        :rtype: List[int]        """        ver = -1        lookup = [ver]*len(nums)        suffix = [0]*len(nums)        ver += 1        mex = 0        for i in reversed(xrange(len(nums))):            if nums[i] < len(lookup):                lookup[nums[i]] = ver            while mex < len(lookup) and lookup[mex] == ver:                mex += 1            suffix[i] = mex        result = []        ver += 1        mex = 0        j = 0        for i in xrange(len(nums)):            if not suffix[j]:                break            if nums[i] < len(lookup):                lookup[nums[i]] = ver            while mex < len(lookup) and lookup[mex] == ver:                mex += 1            if mex != suffix[j]:                continue            result.append(mex)            ver += 1            mex = 0            j = i+1        result.extend(0 for _ in xrange(len(nums)-j))        return result  # Time:  O(n)# Space: O(n)# hash table, freq table, greedyclass Solution2(object):    def maximumMEX(self, nums):        """        :type nums: List[int]        :rtype: List[int]        """        ver = -1        lookup = [ver]*len(nums)        cnt = [0]*len(nums)        ver += 1        mex = 0        for i in xrange(len(nums)):            if nums[i] < len(lookup):                lookup[nums[i]] = ver                cnt[nums[i]] += 1            while mex < len(lookup) and lookup[mex] == ver:                mex += 1        new_suffix = suffix = mex        result = []        ver += 1        mex = 0        j = 0        for i in xrange(len(nums)):            if not suffix:                break            curr = 0            if nums[i] < len(lookup):                lookup[nums[i]] = ver                cnt[nums[i]] -= 1                if not cnt[nums[i]] and nums[i] < new_suffix:                    new_suffix = nums[i]            while mex < len(lookup) and lookup[mex] == ver:                mex += 1            if mex != suffix:                continue            result.append(mex)            ver += 1            mex = 0            j = i+1            suffix = new_suffix        result.extend(0 for _ in xrange(len(nums)-j))        return result 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗