Move Zeroes (LeetCode 283) | Full Solution explained with examples | Study Algorithms

  Рет қаралды 14,214

Nikhil Lohia

Nikhil Lohia

Күн бұрын

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

Пікірлер: 31
@reactninja6567
@reactninja6567 2 жыл бұрын
Hands Down this is the best soln i have seen so far thanks! keep on going.
@supershinobi7892
@supershinobi7892 Жыл бұрын
you have made my coding journey very easy and understandable
@abhinavd2
@abhinavd2 Жыл бұрын
one of the best teacher ever studied😍
@saichaithrik7134
@saichaithrik7134 5 ай бұрын
Your way of teaching is one the best i have ever seen thank you sir
@deeptidip9864
@deeptidip9864 Жыл бұрын
Such an easy explanation💫
@HarshPatel-hu5sr
@HarshPatel-hu5sr 11 ай бұрын
I honestly love your way of teaching, you would make an AMAZING professor
@nikoo28
@nikoo28 11 ай бұрын
Wow, thank you!
@LuckyAndDad
@LuckyAndDad Жыл бұрын
Thank you brother
@UjjwalRaj-CSE-
@UjjwalRaj-CSE- Ай бұрын
I am watching your video first time, you are great
@viveknamdev2605
@viveknamdev2605 6 ай бұрын
Nice Explanation by you
@pulastyadas3351
@pulastyadas3351 Жыл бұрын
You are really awesome❤
@ramielwan48
@ramielwan48 Жыл бұрын
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 😅
@nikoo28
@nikoo28 11 ай бұрын
That is so nice of you. :)
@dewanshanand1395
@dewanshanand1395 2 жыл бұрын
Amazing !!
@TechnicalMasterAman
@TechnicalMasterAman Жыл бұрын
under if condition, we can just do swap(nums[i],nums[insertPos])
@Sakeer89
@Sakeer89 Жыл бұрын
Very easy solution
@ajayupadhyay4964
@ajayupadhyay4964 Жыл бұрын
you are a really great teacher !!
@nikoo28
@nikoo28 Жыл бұрын
Wow, thank you!
@khaleabhishek5023
@khaleabhishek5023 Жыл бұрын
amazing keep going
@shubhamkumar-um2xi
@shubhamkumar-um2xi 4 ай бұрын
Great brother
@shubhamkumar-um2xi
@shubhamkumar-um2xi 4 ай бұрын
Very nice
@sammedpatil3907
@sammedpatil3907 8 ай бұрын
Very Nice Explanation
@nikoo28
@nikoo28 8 ай бұрын
Thanks and welcome
@karthik-varma-1579
@karthik-varma-1579 3 ай бұрын
nikhil awaiting for the sheet that you said will release!!!
@nikoo28
@nikoo28 2 ай бұрын
Will release it soon
@tufanpondu9511
@tufanpondu9511 Жыл бұрын
Sir you make me bound to subscribe to your channel and give like.😍😍😍😍
@nikoo28
@nikoo28 Жыл бұрын
glad to have you as my audience :)
@alexivuckovich8005
@alexivuckovich8005 2 ай бұрын
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?
@nikoo28
@nikoo28 Ай бұрын
O(2n) simplifies to O(n)
@Vasoya_Jaydeep
@Vasoya_Jaydeep 4 ай бұрын
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*/
@rajdeepdas8393
@rajdeepdas8393 3 ай бұрын
Just what the fuck happened......
Incredible magic 🤯✨
00:53
America's Got Talent
Рет қаралды 48 МЛН
Жайдарман | Туған күн 2024 | Алматы
2:22:55
Jaidarman OFFICIAL / JCI
Рет қаралды 1,7 МЛН
Move Zeroes - Leetcode 283 - Python
8:15
NeetCode
Рет қаралды 67 М.
❌ Don't Run Behind 500 LEETCODE Problems ❌ Focus on QPCD
8:31
Max Consecutive Ones (LeetCode 1004) | Full Solution w/ animations
14:41
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 614 М.