First Missing Positive | LeetCode 41 | C++, Java, Python | BITWISE operation
Vložit
- čas přidán 29. 09. 2020
- LeetCode Solutions: • LeetCode Solutions | L...
Github Link: github.com/KnowledgeCenterYou...
*** Best Books For Data Structures & Algorithms for Interviews:*********
1. Cracking the Coding Interview: amzn.to/2WeO3eO
2. Cracking the Coding Interview Paperback: amzn.to/3aSSe3Q
3. Coding Interview Questions - Narasimha Karumanchi: amzn.to/3cYqjkV
4. Data Structures and Algorithms Made Easy - N. Karumanchi: amzn.to/2U8FrDt
5. Data Structures & Algorithms made Easy in Java - N. Karumanchi: amzn.to/2U0qZgY
6. Introduction to Algorithms - CLR - Cormen, Leiserson, Rivest: amzn.to/2Wdp8rZ
*****************************************************************************
September LeetCoding Challenge | Problem 30 | First Missing Positive | 30 September,
Facebook Coding Interview question,
google coding interview question,
leetcode,
First Missing Positive,
First Missing Positive c++,
First Missing Positive Java,
First Missing Positive python,
First Missing Positive solution,
41. First Missing Positive,
#CodingInterview #LeetCode #Google #Amazon
I'm damn sure no better solution is available than this. Thanks a ton, sir ji! I was about to give up but found your video. Keep continue this series.
Hi, Did you stoped making newer videos on Leetcode problems?
Not stopped. But, will be adding selectively.
Great explanation Sir.Thanks
Nice. I finally get it. Your visuals are perfect.
Glad it helped.
Really loved your explanation among all the other CZcams channels present. I was only halfway through this video and it gave me enough confidence and clarity to solve this question by my own. Keep working on the good stuff 😄
Sir , Excellent video , but sir I did not understand why do we have to do x=abs(arr[i ]) ??
As we already put 1 for the negative values In the above loop ? So why we are again checking for positive??
In between while traversing we are making the numbers -ve by multiplying with -1. So we take abs
so good!
Thanks.
Sir ,I write the same code as you, it ran but my program didn't submit
@Knowledge Center, It will be great if you can add leetcode problem "Word Break"
Word Break can be solved using recursion + memoization:
memo = {}
wordDict = set(wordDict)
def solve(s, wordDict):
if s in wordDict:
return True
if s in memo:
return memo[s]
n = len(s)
for i in range(1, n+1):
if (s[:i] in wordDict) and solve(s[i:], wordDict):
memo[s] = True
return True
memo[s] = False
return False
solve(s, wordDict)
Awesome
Thanks.
But it will not work if there has already negative number or 0 in array
Please do make video on Leetcode Challenge September 29 - Word Break.
How the heck can you figure this out without seeing the dang solution for O(1) time. Crazy
why can't we use mex here ?
nice explanation
Thanks.
there is an issue in your logic
updated your logic, just checking the input array has 1 or not, in that case return 1 as an output
public int FirstMissingPositive(int[] nums)
{
int smallestPositive = int.MaxValue;
int length = nums.Length;
// Step 1 -
// loop entire array and mark the cells as 1 wherever cell value is N
for (int i = 0; i < length; i++)
{
if(nums[i] > 0)
{
smallestPositive = Math.Min(smallestPositive, nums[i]);
}
if (nums[i] length)
{
nums[i] = 1;
}
}
if (smallestPositive != 1)
{
return 1;
}
// Step 2 -
// loop entire array and for each cell value -1 position, update to negetive to mark that value is present in range of 1-N
for (int i = 0; i < length; i++)
{
int val = Math.Abs(nums[i]);
if(nums[val - 1] > 0)
nums[val - 1] *= -1;
}
// Step 3 -
// loop entire array, any value as positive , that position + 1 is the missing element, if not found then N+1 is the missing element(eg- (1,2,3,4,5)=>O/p-6)
for (int i = 0; i < length; i++)
{
if (nums[i] > 0) return i + 1;
}
return length + 1;
}
not working for input - 7,8,9, 11, 12
It will work as he wrote a condition to check if 1 is present or not..if 1 is not present then 1 is already returned
I think the problem really starts after 6 minutes
4 for loops 😐...I did it using one while loop and one for loop.....
I did the same way could you elaborate your approach?
why are we converting negavtives into 1 .
Because we can't have negative index
It's very annoying questions