Remove Element - Leetcode 27 - Python

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


0:00 - Read the problem
1:30 - Drawing Explanation
6:52 - Coding Explanation

leetcode 27

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

It's bizarre how many in-place problems you can solve using these exact lines of code (and some variant of the quicksort partition). Even more bizarre are the number of array problems that you can optimize for space complexity by turning them into in-place problems. Thank you so much!! I'm watching your videos concurrently with the Leetcode Arrays explore card, and everything is just starting to click and I can't believe I was just going into interviews this whole time without understanding how systematic your approach should be during interviews. I just practiced a ton and hoped I'd get lucky with stuff I remembered - but now I can reliably solve entire categories of problems with confidence every single time I see them in practice.

servantofthelord
Автор

Thank God, finally found someone who explains the concept so clearly

sekharsamanta
Автор

what is the time complexity of growth of this channel :)

veliea
Автор

Thank you for the wonderful explanation. If you just use [for n in nums:] instead of [for i in range(len(nums):] it slightly improves the runtime as you avoid redundant array element access using indexes.

wonoh
Автор

This is really smart, I didn't think about this at all because I didn't understand the question, I thought it wanted us to SWAP the val with non val, but in truth, it doesn't really matter if val stays in the array or not, just that there is no val infront of non val, and we return the correct number of non vals.

austindavoren
Автор

The little disadvantage I see on this that is giving it such low score is exactly what was mentioned, that this is gonna perform the swap every time regardless of if its needed or not

JEVSant
Автор

The explanation was excellent and I understand everything, the code wondering why it worked 🥺

mwnkt
Автор

In this case I used replace() in my Python solution since replace() is an in-place method. Resulted in a 4-line solution. Memory complexity was O(1) and runtime was 0ms (100%)

haltersweb
Автор

2 pointers is much better

Runtime: 28 ms, faster than 94.80% of Python3 online submissions for Remove Element.
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
A = nums
if len(A)<1:
return len(A)
L, R=0, len(A)-1
c = 0
while L<=R:
if A[R]==val:
R-=1
c+=1
elif A[L]!=val:
L+=1
elif A[L]==val:
A[L], A[R]=A[R], A[L]
L+=1
R-=1
c+=1
return len(A)-c

yustas
Автор

You can make it faster by swapping non-val numbers from the back of the array. Just use another pointer from the back of the array. Stop the swapping when your "front pointer" is greater than or equal to your "back pointer". Hope that helps

entrastic
Автор

Seems similar to partition method of quick sort partition.

bablikumari-gpce
Автор

NeetCode you are the best I so much trust and believe you
Thanks for sharing all of this for free

idrisisah
Автор

class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
k = 0
for i in range(len(nums)):
if val in nums:
nums.remove(val)
k += 1
return k
How about this, it worked tho

ashfanus
Автор

# Take an easy solution for this problem

class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
i = 0
l = len(nums)
while i < l:
if nums[i] == val:
# Just pop the element if it's equal to val
nums.pop(i)
l -= 1
else:
i += 1
return len(nums)

shuddhendushekharmishra
Автор

I think biggest reason that this question has so many down votes is mutability of arrays since they are "fixed sized" data structure.

Автор

i wrote it exactly like yours but my mistake was that i was returning ''nums'' and kept giving me errors

mmmk
Автор

Brother, I am here to learn. In my perception, I think there may a problem when we use [3, 2, 2, 3] according your code.Waiting for your reply

mohammodroby
Автор

I solved it using a while loop which would simply remove the element from the list if the number is equal to val, otherwise it would increment the index. then we will finally return the index.

usamahussain
Автор

I have learnt this from your other videos, Why didn't you use 2 pointers solution which is O( n - number_of_occurrence_of_val) instead of O(n) ? any thoughts ?

swarnendudutta
Автор

Why can’t you just splice the element out since the other non-k elements will just fall in place when you splice the array? I don’t think that removes the O(n)

azikkii