Remove Element - Leetcode 27 - Python
Ғылым және технология
🚀 neetcode.io/ - A better way to prepare for Coding Interviews
🐦 Twitter: / neetcode1
🥷 Discord: / discord
🐮 Support the channel: / neetcode
⭐ BLIND-75 PLAYLIST: • Two Sum - Leetcode 1 -...
💡 CODING SOLUTIONS: • Coding Interview Solut...
💡 DYNAMIC PROGRAMMING PLAYLIST: • House Robber - Leetco...
🌲 TREE PLAYLIST: • Invert Binary Tree - D...
💡 GRAPH PLAYLIST: • Course Schedule - Grap...
💡 BACKTRACKING PLAYLIST: • Word Search - Backtrac...
💡 LINKED LIST PLAYLIST: • Reverse Linked List - ...
💡 BINARY SEARCH PLAYLIST: • Binary Search
📚 STACK PLAYLIST: • Stack Problems
Problem Link: leetcode.com/problems/remove-...
0:00 - Read the problem
1:30 - Drawing Explanation
6:52 - Coding Explanation
leetcode 27
This question was identified as an interview question from here: github.com/xizhengszhang/Leet...
#microsoft #interview #python
Disclosure: Some of the links above may be affiliate links, from which I may earn a small commission.
Пікірлер: 74
Thank God, finally found someone who explains the concept so clearly
@romeorivera9314
11 ай бұрын
I didn't even knew what 'in-place' actually meant :/ (At least I know now)
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
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.
what is the time complexity of growth of this channel :)
@moulee007
2 жыл бұрын
exponential
@leeroymlg4692
Жыл бұрын
O(1)
The explanation was excellent and I understand everything, the code wondering why it worked 🥺
NeetCode you are the best I so much trust and believe you Thanks for sharing all of this for free
Seems similar to partition method of quick sort partition.
nice explanation !!!
you're a legend, man
Thanks!
i wrote it exactly like yours but my mistake was that i was returning ''nums'' and kept giving me errors
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 ?
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.
@imrozkhan71
Ай бұрын
i solved the first test case but wasn't able to solve the second
thanks for the explanation
I think biggest reason that this question has so many down votes is mutability of arrays since they are "fixed sized" data structure.
@pinakadhara7650
Жыл бұрын
Can you please explain? I didn't understand
Жыл бұрын
@@pinakadhara7650 In general, arrays are fixed sized. That means, you can't (or at least too expensive) expand or shrink them. You can change their values but their size will be the same. en.wikipedia.org/wiki/Array_(data_structure) In past, the description was like: "Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory." which a lot of users were complained about this explanation.
@gradientO
Жыл бұрын
@ description is quite clear. It doesn't expect us to change the length of array. After changing elements, we'd return the *size* of the array without the `val` elements , not a new array
Жыл бұрын
@@gradientO Old description was quite unclear, looks like it is updated.
thx!
This is genius!
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
hi your video of Leetcode 2002 is now a wrong answer. always getting TLE now
Similar to Move Zeroes to end.
@NeetCode
2 жыл бұрын
Yeah, exactly!
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)
@nklido
6 ай бұрын
Worst-case scenario of splice is O(n) because the remaining elements need to be shifted
you can add another condition to make it even faster: if (nums[i] !== val) { if (k !== i) { nums[k] = nums[i]; } k++; }
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
@mohammodroby8346
Ай бұрын
after simulate i found 2,2,2,3 . and the value of k is 2(This part is right)
The example 2 output should be [0,1,3,0,4]
❤
# 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 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)
@fusehchan4775
6 ай бұрын
im pretty sure .pop() updates the length of nums so doing "I -= 1" after "nums.pop(i)" isn't necessary right? I might be mistaken but I actually did the exact same thing you did without that
//My solution going backwards through public int removeElement(int[] nums, int val) { int last = nums.length - 1; //go through and just swap the val we dont want to the end pointer for(int i = nums.length - 1; i >= 0; i--){ if(nums[i] == val){ nums[i] = nums[last]; nums[last] = -1; last--; } } return last+1; }
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)
@VadimSchulz
Жыл бұрын
beautiful
@TheIaTaI
Жыл бұрын
Why is the above code much faster than the solution in the video?
@leeroymlg4692
Жыл бұрын
nums[:] = [x for x in nums if x != val] the best solution in terms of maintainability
@themidwife1624
Жыл бұрын
Can you not just forego defining c since L will always end up on the first target element, so it always equals the number of non targets. Also, the way L and R are defined takes care of the 'empty' edge case, so 'if len(A) < 1' is not needed.
@0yustas0
Жыл бұрын
@@themidwife1624 just 3 lines inside loop: def removeElement(self, nums: List[int], val: int) -> int: A = nums L,R = 0,len(A)-1 while L
Omg bro...I tried to do this, ended up having 24 lines of code and you do it in 8 lmao
@zfarahx
2 ай бұрын
practice makes perfect, keep going 👍🏻
I don't understand how you did this
Hey bro i just started with leetcode and i can't solve any questions even the easy one. How do i approch a problem man. Can you please make a videos in it. I feel dumb.
@PAUL-ky4dq
2 жыл бұрын
Same haha. Ironically I already made many huge web applications
@JEVSant
Жыл бұрын
@@PAUL-ky4dq Funny enough, the skills you're gonna end up using during the job have little or nothing to do with what they ask in the interviews. Sometimes it feels like you're just memorizing the answers to standardized questions
@sancho608
Жыл бұрын
hey pal. I still feel that way too. I found a solution to this code that has 96% memory and 90% run time and it is very simple to understand. while a value is in the list, remove that value. then return the length of the list without the value. Makes sense? def removeElement(self, nums: List[int], val: int) -> int: while val in nums: nums.remove(val) return len(nums)
@Azikkii
7 ай бұрын
You shouldn’t look at it as “easy” even though it says easy. Easy means there is usually a super simple way to do it but if you do it in some elaborate way it’s not easy. Hope that makes sense. I actually do better with the medium problems bc I’m not great at noticing subtle patterns and mediums are more in line with my overthinking of every problem.
While (val in nums): nums.remove(val) len(nums) Is this wrong?
@zg5828
2 жыл бұрын
..... you forgot k += 1. When I attempted the question, it told me to return k. :/
class Solution(object): def removeElement(self, nums, val): for v in nums[:]: if v==val: nums.remove(v) return len(nums) This is faster than 70% of answers
@EIpshitaRay
2 жыл бұрын
how do you give list as input though
@EIpshitaRay
2 жыл бұрын
class Solution(object): def removeElement(self, nums, val): for i in range(0,len(nums)): if val in nums: nums.remove(val) print(nums) c1 = Solution() nums = [3,2,2,3] val = 2 c1.removeElement(nums,val)
@eugeneb9256
Жыл бұрын
This modifies the array so it wont work in a real interview even if leetcode accepts it
@blank2588
8 ай бұрын
Bro forgot the point of doing leetcode 🗿🗿🗿🗿🗿
@ekcelhenrichekoumelong4457
3 ай бұрын
"v in nums" is O(n), also "nums.remove()" is O(n) so in the worth case, let's say, nums is only composed of "val" (nums = [val, val, val...]), you solution's time complexity is O(n^2).
im an idiot
Thanks!