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.