LeetCode 283: Move Zeroes - Interview Prep Ep 53

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


⭐ Support my channel and connect with me:

Solutions explained:

Solution1: We could use an extra array to copy the non-zero elements first, and then append all zeroes in the end, time complexity: O(n), space complexity: O(n). But this is not meeting the two requirements in the note.

Solution 2: We could use two pointers: slow and fast: the slow pointers always points at the earliest zero element while the fast pointer always points at the next non-zero element, and we'll keep swapping them as long as the fast pointers detects a non-zero element. Time: O(n), Space: O(1).

// TOOLS THAT I USE:

// MY FAVORITE BOOKS:

My ENTIRE Programming Equipment and Computer Science Bookshelf:

And make sure you subscribe to my channel!

#softwareengineering #leetcode #algorithms #coding #interview #SDE #SWE #SiliconValley #programming #datastructures

Your comments/thoughts/questions/advice will be greatly appreciated!
Рекомендации по теме
Комментарии
Автор

Clean code and very clear explanation! Thanks.

aswiniventrapragada
Автор

👀 faster than %100 of the submissions. 😮

adolfocarrillo
Автор

I am really thankful to you man nobody explained that clearly how to move the pointers adress to a non zero value and all like this video cleared many points for me thank you so muchh😄😄😄

sanetiger
Автор

Very well explained, after looking at a few other explanations, this by far was the easiest to understand, also this helped me with understanding two pointers. Thank you!

Priyanka-lxxw
Автор

best explanation on this one i have ever seen. Thank you!

CS-tmzh
Автор

Thanks for the video explanation! I made some minor changes to your approach and was wondering if this would necessarily be better in terms of "minimizing steps". Doing an if check instead of swapping unnecessarily and since j >= i, we don't need a temp variable.
public void moveZeroes(int[] nums) {
for (int i = 0, j = 0; i < nums.length && j < nums.length; j++) {
if (nums[j] != 0) {
if (i != j) {
nums[i] = nums[j];
nums[j] = 0;
}
i++;
}
}
}

samuelchen
Автор

Hey, Can you explain How we can calculate time complexity and Space complexity of program? any vidoes have you made?

divyamaheshwari
Автор

What is the time and space complexity for this solution??

nikitaarora
Автор

Why I got array index out of bond same code

justworkfine
Автор

How long did it take you to figure out?

mrjosephDJ