Tensorum
YouTubeशिकायला सुरुवात करा
LeetCode 41 · Cyclic Sort / Index Marking

First Missing Positive

LeetCode 41 · कठीण · उपाय, intuition आणि टप्प्याटप्प्याने वॉकथ्रू

Use indices as a presence map (sign marks presence).

कठीणArrayHash Table

मी First Missing Positive (LeetCode 41) असा सोडवला — Cyclic Sort / Index Marking पॅटर्नमधील एक कठीण प्रॉब्लेम. खाली: intuition, टप्प्याटप्प्याने approach, माझा accepted Python कोड, animated test cases आणि line-by-line debugger.

खालील विवरण इंग्रजीत आहे.

Intuition

The answer is always in 1..n+1, so only values in that range matter. A boolean table of size n+1 shows which are present. To do it in O(1) space, reuse the array itself: clamp junk values to 1, then mark value v as present by making nums[v] negative.

Approach

  1. 1

    If 1 is missing, the answer is 1.

  2. 2

    Replace values ≤ 0 or > n with 1 (harmless now).

  3. 3

    For each value v, negate nums[v] (nums[0] stands for n).

  4. 4

    The first index still positive is the answer; otherwise n or n+1.

मी कसा सोडवला · पहिल्या कल्पनेपासून अंतिम उपायापर्यंत

1
Presence tableO(n) time · O(n) space

Boolean array of 1..n.

2
In-place sign markingAcceptedO(n) time · O(1) space

The array is its own hash table.

Presence tablePythonO(n) time · O(n) space
class Solution:
    def firstMissingPositive(self, nums: List[int]) -> int:
        n=len(nums)
        dp = [False]*(n+1)
        for i in range(n):
            if nums[i]>0 and nums[i]<n+1:
                dp[nums[i]]=True
        for i in range(1,n+1):
            if dp[i]==False:
                return i
        return n+1
In-place sign markingPythonO(n) time · O(1) space
class Solution:
    def firstMissingPositive(self, nums: List[int]) -> int:
        #using the same array
        #use the index as key, if nums[num] exists it should be negative, else it should be positive
        #scale the number between 1 to n
        #nums[0]=-1 if n exists else 1
        #before it check whether 1 exist or not
        has_1 = False
        n = len(nums)
        for i in range(n):
            if nums[i]==1:
                has_1=True
            elif nums[i]<=0 or nums[i]>n:
                nums[i]=1
        if not has_1:
            return 1
        for i in range(n):
            num = abs(nums[i])
            if num==n:
                nums[0]=(-1)*abs(nums[0])
            else:
                nums[num]=(-1)*abs(nums[num])
        for i in range(1,n):
            if nums[i]>0:
                return i
        if nums[0]>0:
            return n
        return n+1
स्टेप-थ्रू लोड होत आहे…

First Missing Positive — वारंवार विचारले जाणारे प्रश्न

First Missing Positive मागील intuition काय आहे?

The answer is always in 1..n+1, so only values in that range matter. A boolean table of size n+1 shows which are present. To do it in O(1) space, reuse the array itself: clamp junk values to 1, then mark value v as present by making nums[v] negative.

First Missing Positive (LeetCode 41) कसा सोडवावा?

1. If 1 is missing, the answer is 1. 2. Replace values ≤ 0 or > n with 1 (harmless now). 3. For each value v, negate nums[v] (nums[0] stands for n). 4. The first index still positive is the answer; otherwise n or n+1.

First Missing Positive ची time आणि space complexity किती आहे?

Time: O(n). Space: O(1) in the final version. (Accepted approach: In-place sign marking.)

First Missing Positive मध्ये सामान्य चुका कोणत्या?

Use abs() when reading values — they may already be negated.

First Missing Positive सोडवण्याचे एकापेक्षा जास्त मार्ग आहेत का?

Presence table (O(n) time · O(n) space): Boolean array of 1..n. In-place sign marking (O(n) time · O(1) space): The array is its own hash table.