first missing positive | first missing positive leetcode | leetcode 41 | array

preview_player
Показать описание


#Array #First #Missing #Positive #First_Missing_Positive #Code #Interview #Practice #September #Leetcode #Hard #41 #Algorithm #DataStructure #Java #Preparation #NG #nickode #CookCodeTravel #CCT
Рекомендации по теме
Комментарии
Автор

How was your experience with the September challenge?

NareshGupta
Автор

how can id ever be negative? We have converted all negative and >n numbers to n+1 in above for already.

wisdomkhan
Автор

great explanation
in first attempt i did it with hashSet and I was very happy then I read that do it without extra space.
come to your solution honestly mind blowing.

aman
Автор

JS implementation


var firstMissingPositive = function(nums) {
let length = nums.length;
if(!nums.includes(1)) {
return 1;
}
for(let index=0;index<length;index++) {
{
nums[index]=1;
}
}
for(let index=0;index<length;index++) {
let INDEX = Math.abs(nums[index])-1;
if(nums[INDEX]<0) continue;
nums[INDEX] = 0-nums[INDEX];
}
for(let index=0;index<length;index++) {
if(nums[index]>0) {
return index+1;
}
}
return length+1;
}

ar-rahman
Автор

not correct solution, even boundary conditions not handled, no check for 1 present or not

ar-rahman
Автор

If array is [5, 7, 8] with your solution after first step array is [4, 4, 4], I don't think your approach is right

ar-rahman