<p>Given an unsorted integer array <code>nums</code>. Return the <em>smallest positive integer</em> that is <em>not present</em> in <code>nums</code>.</p>

<p>You must implement an algorithm that runs in <code>O(n)</code> time and uses <code>O(1)</code> auxiliary space.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,0]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The numbers in the range [1,2] are all in the array.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [3,4,-1,1]
<strong>Output:</strong> 2
<strong>Explanation:</strong> 1 is in the array but 2 is missing.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [7,8,9,11,12]
<strong>Output:</strong> 1
<strong>Explanation:</strong> The smallest positive integer 1 is missing.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li>
</ul>

class Solution:
    def firstMissingPositive(self, nums: List[int]) -> int:
        # heapify(nums)
        
        
        # p=heappop(nums)
        # while nums and p<0 :
        #     p=heappop(nums)
        # print(p)
        # start=min(nums)
        # if p:
        #     start=min(start,p)
        # if start>1:
        #     return 1
        # while nums:
        #     c=heappop(nums)
        #     if c-p>1:
                
        #         return p+1
        #     p=c
        # if p+1<1:
        #     return 1
        # return p+1
        heapify(nums)  # Ensure nums is a heap

        if not nums:  # Handle empty input
            return 1

        p = heappop(nums)  # Get the smallest element
        while nums and p < 0:
            p = heappop(nums)  # Keep popping until we find a non-negative number

        # If nums was all negative or empty
        if p < 0:
            return 1

        start = float('inf')  # Initialize start with a large number
        if nums:
            start = min(nums)  # Get the minimum of the remaining numbers

        # Update start based on p
        if p < start:
            start = p

        if start > 1:  # If the smallest is greater than 1
            return 1

        while nums:
            c = heappop(nums)
            if c - p > 1:  # Check the gap between current and previous
                return p + 1
            p = c

        return p + 1    
24553