Leetcode 41. Missing first positive number | Python

Time:2021-2-23

41. Missing first positive number


Title Source: leetcode https://leetcode-cn.com/problems/first-missing-positive

subject


To give you an unsorted integer array, please find the smallest positive integer that does not appear in it.

Example 1:

Input: [1,2,0]
Output: 3

Example 2:

Input: [3,4, - 1,1]
Output: 2

Example 3:

Input: [7,8,9,11,12]
Output: 1

Tips:

  • The time complexity of your algorithm should be o (n), and only constant level extra space can be used.

Thinking of solving problems


Idea: Exchange

Because the topic requires that [the time complexity of the algorithm should be o (n), and only constant level extra space can be used].

If there is no such restriction, you can use the hash table method:

  • The array elements are stored in the hash table and traversed from the beginning to determine whether they are in the hash table (but the time complexity of this method is O (n), but the space complexity is O (n), which does not meet the premise)

But after careful examination, you will find that the array given by the title is not sorted. Then, if the given array is restored, the missing positive number can be found by traversing.

In a given array, the integer we need to find must fall in the interval [1, N + 1] (n represents the length of the array). But n + 1 is not required, because when the number of N faces is not satisfied, the requirement is naturally n + 1.

Let’s look at example 2

Input: [3,4, - 1,1]
Output: 2

In this example, we recover it, and the array is like this: [1, – 1, 3, 4], so we can find that the missing number in the array is 2.

Now the question is how to recover. The general idea is as follows: put 1 in the position with index 0, 2 in the position with index 1, and so on.

That is to say, let the current traversal element be a, that is, a = nums [i]. If a falls in [1, n], according to the above idea, then a should fall in the index of [1, n]a-1The location of the car. At this time, exchangenums[i]andnums[a-1]Then a will be in the right place. However, if the element after the exchange position is still in the range of [1, n], it needs to continue the exchange operation until a no longer falls in [1, n]. The specific process is shown in the figure below:

Leetcode 41. Missing first positive number | Python

Here we need to pay attention to prevent falling into a dead cycle. If a is already in the right position, the above method will always be exchanged. So note here that when the element appears in the correct position, it doesn’t need to swap and skip, just go to the next element.

The specific code implementation is as follows.

code implementation


class Solution:
    def firstMissingPositive(self, nums: List[int]) -> int:
        length = len(nums)

        for i in range(length):
            #Here, judge whether the element falls in [1, n], judge whether it falls in the correct position
            #Note: to prevent entering the dead cycle, when the element just falls in the correct position, skip directly to judge the next element
            while 1 <= nums[i] <= length and nums[i] != nums[nums[i] - 1]:
                #Exchange
                nums[nums[i] - 1], nums[i] = nums[i], nums[nums[i] - 1]
        
        #After restoring the elements of the array to the correct position,
        #Again traversal, when an element is not in the correct position, is to find the first missing positive number, directly return
        for i in range(length):
            if nums[i] != i + 1:
                return i + 1
        #If the elements are all in the right place, then what is missing is n + 1 (length + 1)
        return length + 1

Implementation results


Leetcode 41. Missing first positive number | Python

summary


  • Because the given array is not sorted, and we know that the required missing positive number, then restore the array to the correct position, then when traversing, we will know which positive number is not in the correct position, that is, the missing positive number
  • According to the meaning of the question, we know that the positive number to be found must fall in the interval [1, N + 1] (n represents the length of the array). But n + 1 is not required, because if the front is in the correct position, then the missing is n + 1.
  • How to recover:

    • According to the above, the general idea is: 1 should fall in the position of index 0, 2 should fall in the position of index 1, and so on.
    • Suppose that the current element is a (let a = nums [i], which is easier to understand). If a falls in the interval [1, n], then a should fall in the position of index A-1. Then the exchange position, that is, nums [A-1] and a exchange (substituting a = nums [i], that is, nums [nums [i] – 1] and nums [i]), after exchange, a may still fall in [1, n] until a is no longer in this interval.
    • Here’s a note: don’t fall into a dead cycle. The above premise is that a falls in the [1, n] interval. To put a in the correct position, the following is the place to pay attention to. If a falls in the correct position, the exchange will continue indefinitely. So, when a is in the right place, skip and traverse the next element.

The article is original, I think it’s OK, welcome to pay attention to it. The official account of WeChat is updated in parallel.

Leetcode 41. Missing first positive number | Python