First Missing Positive - Leetcode 41 - Python

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


0:00 - Read the problem
1:35 - Drawing Explanation
16:35 - Coding Explanation

leetcode 41

#sorted #array #python
Disclosure: Some of the links above may be affiliate links, from which I may earn a small commission.
Рекомендации по теме
Комментарии
Автор

In the first round, setting all values less than zero and greater than N with the default value N+1 simplifies the coding a lot.

ygwg
Автор

Thanks for the content. It's the best channel with algorithm explanations. It is explained so clear

ZQutui
Автор

Do people figure these things out by intuition or by studying?

igboman
Автор

Man, this is 1000 times better than the official solution from leetcode Premium.

touwmer
Автор

I paused and solved this problem about 4 mins into the video. You have an amazing way of opening one's eyes to insights. Thanks for sharing your knowledge.

paularah
Автор

The logic used to solve this problem is just insane. I love it ♥ Great explanation!

ameyakale
Автор

This is the best solution and explanation I have ever found, I have been stuck in understanding index as a hash way, especially the “while” statement since I saw this question. And I harshly believe that the leetcode solution is not intuitive as it looks like

cedricchen
Автор

Finally found a best channel coding in phyton .

manubachhal
Автор

Best explanation I've found so far, thanks man

Jul
Автор

Thank you for all the good videos!
One optimization I happened to come across that you missed. Your output range is actually not the length of the array, but the count of positive numbers in it -- a value you can compute very easily in your first loop.

vadimkokielov
Автор

Thank you.

For the edge case where the original value is a zero, in addition to setting something out of bounds, could we also just set it the actual value its represending? E.x. when 2 exists in [-1, 0, ... ], could we set it to -2? That way we dont change the values in our input array since 2 is marked as existing already at some other place.

allen
Автор

Thank you! you are just awesome, please post more leetcode videos before I get accepted by Microsoft :)

rentianxiang
Автор

Dude, I couldn't do this hard question before, so I practiced a lot of the other medium questions and watched your videos. Then, I came back to this problem, and ended up figuring this out by myself! TYSM!!!

frida
Автор

Today I solved this problem and another problem called game of live, these two problems are difficult to figure out a solution with optimal space complexity because we need to store 2 pieces of information in a single figure, which is quite counter-intuitive.

linli
Автор

It's impossible to come to the optimized solution unless you know it before hand.

ehabteima
Автор

Thanks so much! you made it clear and easy to understand.
One question, shouldn't like 8 be `if 1 <= abs(A[i]) <= len(A) :` instead?

MiguelLopez-xvgf
Автор

Thank you so much for all those videos you are really helping me. Waiting for more videos :)

AsmaeMouradi
Автор

Awesome explanation, I made a few modifications
def first_positive(nums):
n = len(nums)
for i in range(n):
if nums[i] < 1 or nums[i] > n:
nums[i] = n+1

for i in range(n):
if abs(nums[i]) > 0 and abs(nums[i]) <= n:
if nums[abs(nums[i]) -1] > 0:
nums[abs(nums[i]) -1] *= -1

for i in range(n):
if nums[i] > 0:
return i+1

return n+1

lonenrd
Автор

crisp and clear!
Thank you so much! :)

Mr_SSK
Автор

Beautifully done. I did it with a cycle sort type of method it got accepted but wasn't feeling good since it wasn't an O(n) solution. Got relief finally

soumyadeepganguly