First Missing Positive
LeetCode 41 · కఠినం · పరిష్కారం, intuition & దశలవారీ వాక్త్రూ
Use indices as a presence map (sign marks presence).
నేను 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
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.
నేను ఎలా పరిష్కరించాను · మొదటి ఆలోచన నుంచి చివరి పరిష్కారం వరకు
Boolean array of 1..n.
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+1In-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+1First 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.