Find All Numbers Disappeared in an Array - Leetcode 448 - Python

Find All Numbers Disappeared in an Array - Leetcode 448 - Python

NeetCode

2 года назад

44,449 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@theresilientpianist7114
@theresilientpianist7114 - 12.08.2023 15:47

goodquestion

Ответить
@dumbfailurekms
@dumbfailurekms - 23.06.2023 00:38

ok that's fucking cool

Ответить
@boris---
@boris--- - 07.04.2023 12:42

Yeah I can go back to McDonald if this is easy question....

Ответить
@rukaiyakhan2406
@rukaiyakhan2406 - 19.12.2022 16:18

can you please name this algorithm

Ответить
@techsavy5669
@techsavy5669 - 16.12.2022 22:24

Do it with cyclic sort, will be less confusing✌

Ответить
@marekglowacki2607
@marekglowacki2607 - 01.11.2022 23:59

I've devised following solution, but I'm not sure if it still counts as O(1) memory:
class Solution:
def findDisappearedNumbers(self, nums: List[int]) -> List[int]:
nums = [0] + nums
for n in nums:
if nums[abs(n)] > 0:
nums[abs(n)] *= -1

return [i for i,v in enumerate(nums) if v > 0]

Ответить
@amanrai5285
@amanrai5285 - 08.10.2022 10:24

I had a hard time to understand the problem:

My way of understanding is:

We are using the indexes to our advantage as they are in range of n. So we are marking the values at the indexes as -ve to signify(the number in the array) is marked visited by putting a -ve sign on the particular index. Finally we loop through all the elements and find which indexes are not -ve and then return them.

Ответить
@BROOKnim
@BROOKnim - 04.10.2022 16:34

if some people dont understand this very clearly, there is this sorting technique called cyclic sort ( sorts array in O(n) if elements are in range of [1,n] ). look it up , will be useful.

Ответить
@greentea_vsrg
@greentea_vsrg - 30.07.2022 01:59

I feel like doing this in constant space makes it a medium

Ответить
@lilyh4573
@lilyh4573 - 23.04.2022 09:02

How come when you multiply any positive number with a negative number (like -1) it turns negative? I've been told that's how it works in school.... but no one told me why?!

Ответить
@vdyb745
@vdyb745 - 01.04.2022 18:28

Superb !!!

Ответить
@andreytamelo1183
@andreytamelo1183 - 29.03.2022 20:43

Thanks!

Ответить
@amrholo4445
@amrholo4445 - 01.03.2022 02:46

Thanks a lot sir

Ответить
@user-vy7gk2qn9l
@user-vy7gk2qn9l - 18.02.2022 01:07

Amassing solution!!!

Ответить
@cgqqqq
@cgqqqq - 13.02.2022 02:28

does this algo have a name? I saw a couple of problems was solved by the same method but none mentioned the name of the method...

Ответить
@VarunMittal-viralmutant
@VarunMittal-viralmutant - 31.01.2022 20:15

A two liner:
valid_set = set(range(1, len(nums) + 1))
return (valid_set - valid_set.intersection(nums))

Ответить
@balajipattabhiraman
@balajipattabhiraman - 30.01.2022 20:50

Could we sort and see if the index value and the actual value align and if not detect missing value and update an offset on what the next index would contain if there is no next missing value

Ответить
@venkatasundararaman
@venkatasundararaman - 30.01.2022 09:34

Instead of changing them to negative, we can just move the numbers to their index and then finally run a loop to check if the number value at an index is index+1 if not then add that to our result.

Ответить
@shubhk33
@shubhk33 - 25.01.2022 18:44

is return list(set(range(1,len(nums)+1)) - set(nums)) a better solution?

Ответить
@samuelcordova5183
@samuelcordova5183 - 24.01.2022 00:07

please do word pattern II. love your videos

Ответить
@OmarChida
@OmarChida - 23.01.2022 16:11

I immediately though of cyclic sort while reading this problem. Turns out it is the correct solution.

Ответить
@faisalabdulkadir9439
@faisalabdulkadir9439 - 23.01.2022 14:05

I really respect u and all the work u do , u have really helped me a lot , pls can the next problem I solve be the increasing in order search tree leetcode 897, I’m finding it hard to grasp the recursion process

Ответить
@janekmuric
@janekmuric - 23.01.2022 12:26

That's not O(1) space, it's still O(1)

Ответить
@danielcustduran
@danielcustduran - 23.01.2022 06:44

I never figured out this approach. I solved it using sets but this doesn't meet the constraint of space complexity. Great!!!

Ответить
@nikhilchauhan5837
@nikhilchauhan5837 - 23.01.2022 04:39

Constant space problems are always tricky i got this one in interview 😔. Can you make more videos on solving constant space problems

Ответить
@Ryanamahaffey
@Ryanamahaffey - 23.01.2022 00:34

This one was a bit hard to grasp at first until I saw the code!

Great video regardless, but I think a better test case to explain would have been [1, 4, 4, 2].

Seeing you go back and mark idx 1 (for the number 2) as a negative 4 would have made things much clearer for me!

Ответить
@meowmaple
@meowmaple - 22.01.2022 20:52

Previously had done this question but not in the O(1) space complexity way, and wow I must say that this is such an interesting way of making it O(1) space complexity!

Ответить
@vinitkh
@vinitkh - 22.01.2022 20:45

Keep up the leetcode!

Ответить
@leetcodeespanol59
@leetcodeespanol59 - 22.01.2022 20:31

Thanks for all your hard work Neetcode! Its great to see you consistently upload videos for us! Keep it up!

Ответить
@jayeshkaushik2975
@jayeshkaushik2975 - 22.01.2022 20:31

hey, can you please explain the leetcode contest problems?

Ответить
@pranshusharma26
@pranshusharma26 - 22.01.2022 20:26

Can we solve it like, first take the minimum and maximum value present in the given list.Traverse between minimum to maximum value and then return those numbers which are not in given list ???

Ответить
@hussamq9909
@hussamq9909 - 22.01.2022 20:24

Great explanation, you never disappoint. Keep up the good work.

Ответить
@SoyAudio
@SoyAudio - 22.01.2022 20:18

I love you!!!!!!!!

Ответить