Sort Colors (LeetCode 75) | Dutch National Flag Problem | Full Solution with Visuals and Animations

SdĂ­let
VloĆŸit
  • čas pƙidĂĄn 24. 07. 2024
  • Actual Problem: leetcode.com/problems/sort-co...
    Chapters:
    00:00 - Intro
    00:40 - Problem Statement and Description
    02:43 - The most obvious solution
    05:23 - Solving for efficiency in-place
    12:01 - Dry-run of Code
    14:28 - Final Thoughts
    📚 Links to topics I talk about in the video:
    Sorting Techniques: ‱ Sorting Techniques
    Arrays: ‱ Array Data Structure e...
    📘 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 • 57

  • @jameswalt2199
    @jameswalt2199 Pƙed 2 dny

    YOU MADE IT THE SIMPLEST TO UNDERSTAND WITHOUT COMPLICATING THINGS!!!!! HAT'S OFF TO YOU!

  • @Azeem_Idrisi
    @Azeem_Idrisi Pƙed 6 měsĂ­ci +6

    Best solution I've seen for this problem. This channel is so underrated.

  • @unanimous8510
    @unanimous8510 Pƙed měsĂ­cem

    Man that’s the best explanation. I saw the coded solution for this problem which is same as yours but couldn’t wrap my head around it. Now I got it! Thank you!

  • @sarthakgadge5223
    @sarthakgadge5223 Pƙed měsĂ­cem

    Thanks man this helped me a lot, loved your energy throughout the problem.

  • @shwetayadav4244
    @shwetayadav4244 Pƙed 6 měsĂ­ci +1

    your explanations are really amazing. In fact, best so far :) Please make more videos :)

  • @shabanlukyamuzi4012
    @shabanlukyamuzi4012 Pƙed 10 měsĂ­ci

    Best explanation on CZcams for the problem

  • @azharsofi2854
    @azharsofi2854 Pƙed rokem +1

    your teaching is next level

  • @omdongare2005
    @omdongare2005 Pƙed 6 měsĂ­ci +1

    Your the best bro. The problem seems so easy with the way you explain it. Thanks again. Also this is my solution in python based on your approac

  • @mayankbadika3101
    @mayankbadika3101 Pƙed 6 měsĂ­ci +1

    very good explanation! keep up the good work

  • @vinoths7140
    @vinoths7140 Pƙed rokem +1

    Great Explanation, Thank You.

  • @anayatk.007
    @anayatk.007 Pƙed 7 měsĂ­ci +1

    Thank you for providing such fantastic content

  • @omsudhamsh.h
    @omsudhamsh.h Pƙed 2 měsĂ­ci

    Picture perfect mate!
    Thanks.

  • @dylanjara201
    @dylanjara201 Pƙed rokem +1

    Perfect explained. Ty sir!!

  • @BroskiXP
    @BroskiXP Pƙed 2 měsĂ­ci +1

    Great explanation, good work

  • @moezzzz9341
    @moezzzz9341 Pƙed 6 měsĂ­ci +2

    Your the best bro. The problem seems so easy with the way you explain it. Thanks again. Also this is my solution in python based on your approach
    class Solution:
    def sortColors(self, nums: List[int]) -> None:
    """
    Do not return anything, modify nums in-place instead.
    """
    start = 0
    middle = 0
    end = len(nums ) - 1
    while middle

  • @user-yh2vm8ge2u
    @user-yh2vm8ge2u Pƙed 3 měsĂ­ci

    VERY NICE EXPLANATION WITH CLARITY . THANKS BHAIYA .

  • @sithutun688
    @sithutun688 Pƙed 2 měsĂ­ci

    best explanation Sir! I didn't watch the coding part before solving the problem. So I implemented it using if-else statements.After solving, I watched the last part. I found out you wrote it using switch cases. Understanding both versions make me confident in coding

  • @neeeelll_
    @neeeelll_ Pƙed rokem +1

    Beautifully explained. could u please come up with topic based important questions

  • @sumitraj9312
    @sumitraj9312 Pƙed 5 měsĂ­ci

    Thank you, ploblem solved. love you
    😁

  • @user-wj4eg4rx4j
    @user-wj4eg4rx4j Pƙed 9 měsĂ­ci +1

    Underrated!

  • @apex095
    @apex095 Pƙed rokem +1

    elegant solution. could you please add more questions on variety of topics like Stacks and Queues etc on your website ?

  • @arnavkukreti2009
    @arnavkukreti2009 Pƙed 10 měsĂ­ci

    you explain in a very good manner
    thank you

    • @nikoo28
      @nikoo28  Pƙed 10 měsĂ­ci +1

      So nice of you

  • @Harrish30
    @Harrish30 Pƙed rokem

    Perfect!! Thanks

  • @technicalguy.
    @technicalguy. Pƙed 5 měsĂ­ci

    Thank you soo much ❀❀

  • @everyontech2716
    @everyontech2716 Pƙed rokem

    great explanation

  • @sarthakgupta096
    @sarthakgupta096 Pƙed 11 měsĂ­ci

    Thankyou sir your way of teaching is amazing

    • @nikoo28
      @nikoo28  Pƙed 10 měsĂ­ci

      It's my pleasure

  • @jerronjones470
    @jerronjones470 Pƙed 10 měsĂ­ci

    Yay, someone I can understand

  • @subee128
    @subee128 Pƙed 6 měsĂ­ci +1

    Thank you

  • @LokeshSharma-hm5jz
    @LokeshSharma-hm5jz Pƙed 10 měsĂ­ci

    i dont know why i developed a fear for this problem. You made it very easy. Thanks.

    • @nikoo28
      @nikoo28  Pƙed 9 měsĂ­ci +2

      I was once in the same boat as you my friend. :)

    • @user-fq1nn1vc2p
      @user-fq1nn1vc2p Pƙed 17 dny

      @@nikoo28 🙏🙏

  • @iceyyeah
    @iceyyeah Pƙed 5 měsĂ­ci +1

    Thanks

  • @pranavshekhar2048
    @pranavshekhar2048 Pƙed 4 měsĂ­ci

    Great explaination! But I noticed one thing, in the first example, first swap is wrong as middle was at 0, so swap between start and middle should take place and start and middle should move and not between middle and end i.e 0 and 2 what you did because middle was not at 2.

  • @user-un9ri4vt7l
    @user-un9ri4vt7l Pƙed 3 měsĂ­ci

    sir, I actually used in-built sort function, in leetcode ie. sort(nums.begin(),nums.end()), and it said said, u beat 100% users with c++. Can we do this or not???

    • @nikoo28
      @nikoo28  Pƙed 3 měsĂ­ci +1

      You can, but your interviewer and ask you to solve it without sorting.

  • @iiju8212
    @iiju8212 Pƙed rokem

    Bhai kaafi mast samjhaya.

  • @m-bk4um
    @m-bk4um Pƙed 9 měsĂ­ci

    good

  • @CPS_XI
    @CPS_XI Pƙed 4 měsĂ­ci

    đŸ’Żâ€

  • @KarthikC-ju4fx
    @KarthikC-ju4fx Pƙed měsĂ­cem

    ❀

  • @officialdreamplayz
    @officialdreamplayz Pƙed 4 měsĂ­ci

    i used selection sort

  • @user-ik1qr9ez2b
    @user-ik1qr9ez2b Pƙed 5 měsĂ­ci

    why we are not incrementing mid when it is arr[mid] is 2?

    • @nikoo28
      @nikoo28  Pƙed 5 měsĂ­ci +1

      we swap it out, and put the 2 at the end. so we don't increment it. we need to see what came after the swap.

    • @bhargavinaik8145
      @bhargavinaik8145 Pƙed měsĂ­cem

      @@nikoo28 I solved it on leetcode, i knew it would fail for some testcases and it did, I kept trying to understand what was the pattern of those test cases, your answer made understand that exact point. Thanks :)

  • @SubhajitDas-mt7sn
    @SubhajitDas-mt7sn Pƙed měsĂ­cem

    c++
    class Solution {
    public:
    void sortColors(vector& nums) {
    int start = 0;
    int middle = 0;
    int end = nums.size() - 1;
    while (middle

  • @shenth27
    @shenth27 Pƙed 3 měsĂ­ci +1

    why don't we just loop through the entire array and count the 0's and 1's in seperate variables, then loop the array again and replace number of 0, 1, 2 in that order.

    • @DohaZilaoui-zq1gx
      @DohaZilaoui-zq1gx Pƙed 3 měsĂ­ci

      Its just of solution of 10000000000solutions that u should keep it in your mind

    • @nikoo28
      @nikoo28  Pƙed 3 měsĂ­ci

      There are multiple ways to approach the problem. You want to do in the fastest way possible. It cannot get faster than a single scan

    • @unemployedcse3514
      @unemployedcse3514 Pƙed 2 měsĂ­ci

      but interviewer won't be impressed by this approach 😂😂

  • @nenuanenenuane6645
    @nenuanenenuane6645 Pƙed rokem

    // Java code to sort an array of integers
    // with the help of single loop
    import java.util.*;
    class Geeks_For_Geeks {
    // Function for Sorting the array
    // using a single loop
    public static int[] sortArrays(int[] arr)
    {
    // Finding the length of array 'arr'
    int length = arr.length;
    // Sorting using a single loop
    for (int j = 0; j < length - 1; j++) {
    // Checking the condition for two
    // simultaneous elements of the array
    if (arr[j] > arr[j + 1]) {
    // Swapping the elements.
    int temp = arr[j];
    arr[j] = arr[j + 1];
    arr[j + 1] = temp;
    // updating the value of j = -1
    // so after getting updated for j++
    // in the loop it becomes 0 and
    // the loop begins from the start.
    j = -1;
    }
    }
    }
    Bro can we use this as well plz let me know

  • @chessps
    @chessps Pƙed rokem +1

    more easier!!!
    class Solution {
    public static void sortColors(int[] nums) {
    int zero = 0;
    int one = 0;
    int two = 0;
    for (int i: nums) {
    if(i==0){
    zero++;
    } else if (i==1) {
    one++;
    }else {
    two++;
    }
    }
    for (int i = 0; i < nums.length ; i++) {
    if(zero!=0){
    zero--;
    nums[i]= 0;
    } else if (one!=0) {
    one--;
    nums[i]= 1;
    }else if(two!=0) {
    two--;
    nums[i]= 2;
    }
    }
    }
    }

    • @kidoo1567
      @kidoo1567 Pƙed 10 měsĂ­ci

      But complexity?

  • @ankitraj4179
    @ankitraj4179 Pƙed měsĂ­cem

    Java Solution (Beats 100 %)
    class Solution {
    public void sortColors(int[] nums) {
    int n = nums.length ;
    int[] arr = new int[3] ;
    int element = 0 ;
    for(int i = 0 ; i < n ; i++){
    element = nums[i] ;
    arr[element]++ ;
    }
    int count = 0 ;
    int k = 0 ;
    for(int i = 0 ; i 0){
    nums[k] = i ;
    k++ ;
    count-- ;
    }
    }
    }
    }

  • @hwval-zw4hy
    @hwval-zw4hy Pƙed 6 měsĂ­ci

    Why not just count zeroes and ones and refill the array in place? 😂

    • @nikoo28
      @nikoo28  Pƙed 6 měsĂ­ci +2

      You will need to iterate over the array twice. First to count all the different 0 and 1s. Next iteration will be to actually fill all the elements.
      In the approach I discuss, we just do a single scan of the array.

    • @hwval-zw4hy
      @hwval-zw4hy Pƙed 6 měsĂ­ci

      @@nikoo28 I like your solution. One pass is good.
      Though counting involves same big O complexity and simpler approach.
      I also think your solution fits better definition of in-place. E.g. if these were objects, not integers to sort: mine solution wouldn't be acceptable.