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.