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

Sdílet
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

Komentáře • 31

  • @reactninja6567
    @reactninja6567 Před 2 lety +2

    Hands Down this is the best soln i have seen so far thanks! keep on going.

  • @supershinobi7892
    @supershinobi7892 Před rokem +1

    you have made my coding journey very easy and understandable

  • @saichaithrik7134
    @saichaithrik7134 Před 5 měsíci +1

    Your way of teaching is one the best i have ever seen thank you sir

  • @abhinavd2
    @abhinavd2 Před rokem +1

    one of the best teacher ever studied😍

  • @HarshPatel-hu5sr
    @HarshPatel-hu5sr Před 11 měsíci +2

    I honestly love your way of teaching, you would make an AMAZING professor

  • @deeptidip9864
    @deeptidip9864 Před rokem +1

    Such an easy explanation💫

  • @UjjwalRaj-CSE-
    @UjjwalRaj-CSE- Před měsícem

    I am watching your video first time, you are great

  • @viveknamdev2605
    @viveknamdev2605 Před 5 měsíci

    Nice Explanation by you

  • @dewanshanand1395
    @dewanshanand1395 Před 2 lety

    Amazing !!

  • @ramielwan48
    @ramielwan48 Před 11 měsíci +3

    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  Před 11 měsíci +2

      That is so nice of you. :)

  • @LuckyAndDad
    @LuckyAndDad Před rokem +1

    Thank you brother

  • @khaleabhishek5023
    @khaleabhishek5023 Před rokem +1

    amazing keep going

  • @TechnicalMasterAman
    @TechnicalMasterAman Před rokem +1

    under if condition, we can just do swap(nums[i],nums[insertPos])

  • @shubhamkumar-um2xi
    @shubhamkumar-um2xi Před 4 měsíci

    Great brother

  • @pulastyadas3351
    @pulastyadas3351 Před rokem

    You are really awesome❤

  • @ajayupadhyay4964
    @ajayupadhyay4964 Před rokem

    you are a really great teacher !!

  • @shubhamkumar-um2xi
    @shubhamkumar-um2xi Před 4 měsíci

    Very nice

  • @Sakeer89
    @Sakeer89 Před rokem

    Very easy solution

  • @sammedpatil3907
    @sammedpatil3907 Před 8 měsíci

    Very Nice Explanation

    • @nikoo28
      @nikoo28  Před 8 měsíci

      Thanks and welcome

  • @alexivuckovich8005
    @alexivuckovich8005 Před měsícem

    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  Před měsícem +1

      O(2n) simplifies to O(n)

  • @karthik-varma-1579
    @karthik-varma-1579 Před 2 měsíci

    nikhil awaiting for the sheet that you said will release!!!

    • @nikoo28
      @nikoo28  Před 2 měsíci

      Will release it soon

  • @Vasoya_Jaydeep
    @Vasoya_Jaydeep Před 4 měsíci

    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*/

  • @tufanpondu9511
    @tufanpondu9511 Před rokem

    Sir you make me bound to subscribe to your channel and give like.😍😍😍😍

    • @nikoo28
      @nikoo28  Před rokem

      glad to have you as my audience :)

  • @rajdeepdas8393
    @rajdeepdas8393 Před 3 měsíci

    Just what the fuck happened......