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.
Hi. Can we link up. I am a freshman practicing leetcode also. I am grasping the concepts bit by bit, practicing towards a potential interview, and I would love the opportunity to chat with you and gain insights about your development.
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.
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.
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
not true! Assume: nums = [1,2,3] i = 0 j = 0 nums[i] = nums[j] The element of the array in this case "1" is the same(Same memory address), the thing that is changing is th index "i", "j".
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%)
@@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.
@ 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.
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
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)
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.
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 ?
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.
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)
# 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)
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; }
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
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)
"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).
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 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
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)
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.