Move Zeroes (LeetCode 283) | Full Solution explained with examples | Study Algorithms
Vložit
- čas přidán 5. 07. 2024
- Given an array of positive integers with some zeroes. You need to move all the zeroes to the end without changing the relative order of non-zero elements. At the first glance it feels that you will need to swap and move around a lot of elements to arrive at an efficient solution. This video explores a unique way of solving this problem and once you understand, it feels so easy and obvious. Watch the complete video with my final thoughts and a dry-run of code in JAVA.
Actual Problem on LeetCode: leetcode.com/problems/move-ze...
Chapters:
00:00 - Intro
01:12 - Problem statement and description
02:50 - Straight forward approach to move zeroes
04:15 - Space Efficient Solution In-place
06:56 - Dry-run of code
09:22 - Final Thoughts
📚 Links to topics I talk about in the video:
Brute Force Solution: • Brute Force algorithms...
What is Big O?: • Big O Notation Simplif...
Time Complexity: • What is the Time Compl...
More LeetCode problems: • Leetcode Solutions
📘 A text based explanation is available at: studyalgorithms.com
Code on Github: github.com/nikoo28/java-solut...
Test-cases on Github: github.com/nikoo28/java-solut...
📖 Reference Books:
Starting Learn to Code: amzn.to/36pU0JO
Favorite book to understand algorithms: amzn.to/39w3YLS
Favorite book for data structures: amzn.to/3oAVBTk
Get started for interview preparation: amzn.to/39ysbkJ
🔗 To see more videos like this, you can show your support on: www.buymeacoffee.com/studyalg...
🎥 My Recording Gear:
Recording Light: amzn.to/3pAqh8O
Microphone: amzn.to/2MCX7qU
Recording Camera: amzn.to/3alg9Ky
Tablet to sketch and draw: amzn.to/3pM6Bi4
Surface Pen: amzn.to/3pv6tTs
Laptop to edit videos: amzn.to/2LYpMqn
💻 Get Social 💻
Follow on Facebook at: / studyalgos
Follow on Twitter at: / studyalgorithms
Follow on Tumblr at: / studyalgos
Subscribe to RSS feeds: studyalgorithms.com/feed/
Join fan mail: eepurl.com/g9Dadv
#leetcode #programming #interview
Hands Down this is the best soln i have seen so far thanks! keep on going.
you have made my coding journey very easy and understandable
Your way of teaching is one the best i have ever seen thank you sir
one of the best teacher ever studied😍
I honestly love your way of teaching, you would make an AMAZING professor
Wow, thank you!
Such an easy explanation💫
I am watching your video first time, you are great
Nice Explanation by you
Amazing !!
Man I swear I asked chat gpt to explain it to me. I read the leetcode solution, and watched 2 other videos but none of them made sense to me. I wanted to understand how to approach it and not just copy and paste the solution. When you explained it, I was like damn that's easy 😅
That is so nice of you. :)
Thank you brother
amazing keep going
under if condition, we can just do swap(nums[i],nums[insertPos])
Great brother
You are really awesome❤
you are a really great teacher !!
Wow, thank you!
Very nice
Very easy solution
Very Nice Explanation
Thanks and welcome
Loving all the videos so far, but with this one it seems incorrect to say it's O(n) becuase the array is iterated over only once. I think it's actually of O(n) because it's simplified from O(2n) in the worst case that the array is all zeros. What about adding a line a code before inserttPosition is incremented that checks if i > insertPosition and just set the i-th element to zero then?
O(2n) simplifies to O(n)
nikhil awaiting for the sheet that you said will release!!!
Will release it soon
This will take O(n + number of zeros) Time
Here is one more solution with O(n) Time
void moveZeroes(vector& nums) {
int Pi =0 , Zi = 0;
while(Pi < nums.size()) {
if (nums[Zi] == 0)
{
while((Pi < nums.size()) && !nums[Pi]) {
Pi++;
}
if (Pi >= nums.size())
break;
int tmp = nums[Zi];
nums[Zi] = nums[Pi];
nums[Pi] = tmp;
}
Zi++;
Pi++;
}
}
/*End*/
Sir you make me bound to subscribe to your channel and give like.😍😍😍😍
glad to have you as my audience :)
Just what the fuck happened......