Move Zeroes - Leetcode 283 - Python

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


0:00 - Read the problem
3:00 - Drawing Explanation
6:36 - Coding Explanation

leetcode 283

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

I absolutely love your approach and explanation.... Please upload more Amazon interview questions..I have interview coming up....

abhimalyachowdhury
Автор

Thank you for taking the time to do this, much appreciated!

timwebster
Автор

Wish I found this channel before I got this question during my interview

deckardbarnes
Автор

I was confused in this line <if nums[i]> I thought He did not wrote the hole condition but then I realise if 0 is present at ith index it will return fasle and if not then it will reurn true. So we don't hav to write hole condition here. Interesting humm!

PremPal-uynm
Автор

neetcode is the best.. I am thinking of doing the neetcode gauntlet!! Do every problem that neetcode has completed from his first upload!!! Then I will be ready for MANGA

Cruzylife
Автор

If you are confused about where does this patterns fits, read about partition alorithm. Thanks Neetcode.

sameer_sah
Автор

Ugh. I had this so close doing the problem on my own but caught up on some edge cases. Explanation is helpful!

sawyerburnett
Автор

great video! A followup question is how would you keep all the zeros at the beginning and keeping the non zeros in order? E.g. [1, 0, 3, 0, 12] The only way I've found is using your method but start iterating at the end of the array. Wondering if there is another way to solve it. Thanks

pl
Автор

When I attempted myself, I kept trying to push the zeros forward rather than push the non-zeros back. I missed this insight and couldn't solve it.

gremlan
Автор

Hi neetcode, thanks for posting i love ur videos so much! I would appreciate if u can post video on Longest Increasing Path in a Matrix!!

ftrjjhn
Автор

Thanks a ton for simplifying problem statement at beginning because i derived similar logic with input = {0, 0, 0, 12, 4}
but when change input = {8, 2, 0, 1, ...} from java algo tutorial course, i got confused thinking my logic doing swap when leading pos elements are not zero but your simplifying problem here is trick i learn to justify to interviewers

my var name was zeroPos but later renamed to slowPointer but left & right seem more suited :)

dhruva
Автор

integs = [0, 1, 0, 3, 12]
integs.sort()
for x in range(len(integs)-1, -1, -1):
if integs[x] == 0:
a = integs.pop(x)
integs.append(a)

In [47]: integs
Out[47]: [1, 3, 12, 0, 0]

ChaseHatch
Автор

#Easy Python Solution - Two Pointers Approach
class Solution:
def moveZeroes(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
l = 0
for r in range(1, len(nums)):
if nums[l] == 0 and nums[r] != 0:
nums[l], nums[r] = nums[r], nums[l]
l += 1
elif nums[l] != 0:
l += 1

shubhanshusingh
Автор

Unfortunately, your solution doesn't go with this input: nums = [1, 0, 1]

The below solution deals with the abovementioned cases as well:

def move_zeros(arr):
i, j = 0, 1
while j < len(arr):
if arr[i] == 0 and arr[j] != 0:
arr[i], arr[j] = arr[j], arr[i]
i += 1
j += 1
elif arr[i] != 0:
i += 1
j += 1
else:
j += 1

barnoma-soz
Автор

How can " if nums[r]:" mean that number is not zero?

IdiotOfftheInternet
Автор

My notes for this problem

"""
Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements.

Note that you must do this in-place without making a copy of the array.
"""


"""
Approach 1: Using 2 pointer
TC O(n) & SC O(1)

1. Basically, It is 2 pointer quesion. It seems very easy but the way it works is quite interesting.

2. Left pointer is putting every none-zero element at the right place. Wheneve left pointer find zero elemnt it will stick with it.

3. Right pointer is helping left pointer to find and put non-zero element at right place. we can say that right pointer is
showing way to left pointer where to go.

4. What happens if we start right pointer from the second element instead of first?
- In this case it will give wrong output. Because right pointer swap every non-zero item to left pointing item.
so it will chage the order of list.
- Swapping also occurs when right and left pointer both start from 0th position but in this case the swapping of element
happens at the same place. which does not changes the order.



"""

def moveZeroes(nums):
l=0
for r in range(0, len(nums)): #4
if nums[r]:
nums[l], nums[r]=nums[r], nums[l]
l+=1
return nums

print(moveZeroes([0, 1, 0, 3, 12]))

PremPal-uynm
Автор

Nice insights of simple algorithm. Thanks!

atulkumar-bbvi
Автор

when 0s are not present in the list, this method swaps all the elements with itself. However, the following doesn't

var moveZeroes = function(nums) {
let left = 0;
let right = 1;

while(right < nums.length){
if(nums[left]){
left++;
}

if(nums[right] && nums[left] === 0){
const temp = nums[right];
nums[right] = nums[left];
nums[left] = temp;
left++;
}
right++;
}

return nums;
};

prateek
Автор

Thank you for your work, It would be amazing if we can get more than 1 solution, I guess that we have to find the worse and the best solution in an interview. Is this the best one? Cheers!

fabiajero
Автор

Hi, You are doing great job!!....Could you please help in the question -- Making a Large Island (Leetcode 827).
Would Really appreciate that!!
Thanks a lot for all these videos :)

Kushagra_