Find All Duplicates in an Array - Leetcode 442 - Python

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


0:00 - Read the problem
0:22 - Drawing Explanation
8:02 - Coding Explanation

leetcode 442

#neetcode #leetcode #python
Рекомендации по теме
Комментарии
Автор

trick: whenever the question says numbers something about whole numbers until n, see if marking the index by making its value negative helps

ngneerin
Автор

Solved it in O(n) and constant space !! A beautiful problem . The O(n) approach is really elegant and smart.

satyamjha
Автор

Quite a smart way to map the value itself to the respective index position (value - 1) in the nums array since the the array will always contain the numbers ranging from 1 to n.

I couldn't figure it out without using a hash set. Thanks for the solution.

NicholasYg
Автор

After reading cyclic sort technique, this solution make me feel more clear!

leo
Автор

if you use a different property, you can do it in true O(1) space complexity by returning the input array after modification

this involves adding n+1 to elements when visited and using %(n+1) to check further indexes

u can then test for all values greater than 2(n+1) and change them to their index values (note the 1-indexing) and remove from list in O(1) for at worst n items, as relative order doesnt matter and thus removal can be done by swapping to end of array

i think this approach adheres to the spirit of the question much better

:)

lwsqlte
Автор

This is epic. I can never come up with this idea on my own. Despite I use collections.Counter and list comprehension to reach to beat-85% runtime speed, your idea is still brilliant.

Maverick
Автор

i think this is a mistake on their behalf but arent you making an extra array res that in the worst case gonna be of length O(n/2) which simplifies to O(n) ?, also here's a problem that is basically the same as this one for people who wanna solve it (yesterday's problem) : 287. Find the Duplicate Number

pastori
Автор

Should we assume we can use the input array as extra memory when we face this kind of problem in an interview? I thought this might be a bad practice in real world scenarios.

il
Автор

There’s actually a constant space and constant *time* solution. Basically you choose a random index and add that to the result array, then return immediately. The downside is that it has an accuracy complexity of P(1/N^2) avg case and P(0) worst case, however when it works it’s da best!

ChristopherElwell
Автор

already solve it, with my own intuition of cycle sort but I feel its not very efficient although it was O(n) time, now I'm here to get more efficient solution

varunpalsingh
Автор

Thanks for the video. Do you sometimes skip daily questions even if you didn't solve them? Because I don't remember exactly which dates (february or march) but I looked for the daily questions but I couldn't find them.

MehmetDemir-xiyy
Автор

I’d solve this with the hare and tortoise algorithm, mutating the base array to keep a Constant space is hacky for me (and would not to be possible in functional langs), pretty straightforward solution tho

kakaique
Автор

generally speaking if ain't done in-place it's pretty much O(n) extra memory regardless. And can't think of solution less then O(nlogn) time which could swap out dupes somehow.

DeathSugar
Автор

Alternative solution: you walk through the array,

if a[i] - 1 = i, continue.

If a[a[i] - 1] !=a[i], you swap them. Keep swapping until you either unravel the whole cycle, or you find a duplicate. If you find a duplicate, you can mark it and continue.

Very similar, performance characteristics I believe.

mezuzza
Автор

Hi nice solution does this logic work when we have a test case like this n =4 and nums = [3, 3, 3, 3] this would return res =[3, 3] and we would have duplicates in the result array so we may need to add a condition to check if that number is already present in the result list? Thanks in advance @neetcodeio

ashwinkrishnan
Автор

I feel like you’ve done a video before where you use this trick of making values negative, can’t remember what the problem was though

CS_nb
Автор

Find the Duplicate Number Is very similar. It how I solved this question really fast

chrischika
Автор

can you do "126. Word Ladder II"? You have done 127, but this one is more specific and it can easily go to TLE. I cannot find a clear video solution for 127.

ax
Автор

Well when I saw the thumbnail of the video, I went straight to the leetcode and tried solving the problem myself . So thing that was confusing me was that we have to use constant extra space otherwise we could use a HashSet and store all the elements and then check if any element is present twice we push that to the result array or we could iterate through the array twice but that would be O(n2).

So then I thought of the Dictionary(Key-Value) . So we can simply generate a frequency map using a for/foreach loop and keep track of the elements frequencies using a dictionary and when the frequency of an element equal 2 we push that to the result. So sine dictionary is not dependent on input array it's constance space and we are iterating through the array once so it's O(n).

I'm getting better though xD.

salmanpatrick
Автор

This assumes mutability of the input data. You are still using O(n) space since you are "stealing" one bit from each array position to use as a flag and you need n of them.
Its a clever solution to a problem that has a very badly specified question.

rodrigoserafim