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

  Рет қаралды 26,843

Nikhil Lohia

Nikhil Lohia

Күн бұрын

Пікірлер: 73
@Azeem_Idrisi
@Azeem_Idrisi 11 ай бұрын
Best solution I've seen for this problem. This channel is so underrated.
@jameswalt2199
@jameswalt2199 5 ай бұрын
YOU MADE IT THE SIMPLEST TO UNDERSTAND WITHOUT COMPLICATING THINGS!!!!! HAT'S OFF TO YOU!
@themodernmonk7
@themodernmonk7 11 күн бұрын
Wow wow. I watched tons of video on DNF algoritm, I didn't understood whay they are saying. You explaind it excellently.
@shabanashaik2784
@shabanashaik2784 25 күн бұрын
Hi Nikhil!! I am so glad to find this channel. The presentation and the way you explain things in a simple and easy-to-understand manner are awesome. Last but not least, the dry run and the explanation of the code are what I have been looking for so long. I finally found your channel. Thanks a ton, brother.
@nikoo28
@nikoo28 24 күн бұрын
Appreciate the kind words! Keep watching for more.
@azharsofi2854
@azharsofi2854 Жыл бұрын
your teaching is next level
@LokeshSharma-hm5jz
@LokeshSharma-hm5jz Жыл бұрын
i dont know why i developed a fear for this problem. You made it very easy. Thanks.
@nikoo28
@nikoo28 Жыл бұрын
I was once in the same boat as you my friend. :)
@RikitaRoy-m2n
@RikitaRoy-m2n 5 ай бұрын
@@nikoo28 🙏🙏
@unanimous8510
@unanimous8510 6 ай бұрын
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!
@shwetayadav4244
@shwetayadav4244 11 ай бұрын
your explanations are really amazing. In fact, best so far :) Please make more videos :)
@shabanlukyamuzi4012
@shabanlukyamuzi4012 Жыл бұрын
Best explanation on KZbin for the problem
@omdongare2005
@omdongare2005 11 ай бұрын
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
@naol80
@naol80 Ай бұрын
wow. such a simple and wonderful solution
@mayankbadika3101
@mayankbadika3101 11 ай бұрын
very good explanation! keep up the good work
@sarthakgadge5223
@sarthakgadge5223 6 ай бұрын
Thanks man this helped me a lot, loved your energy throughout the problem.
@sithutun688
@sithutun688 7 ай бұрын
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
@captainstring3820
@captainstring3820 3 ай бұрын
really very good explanation. you made it so easy
@Aspiringactor02
@Aspiringactor02 4 ай бұрын
This is better than strivers i watched him then i watched you had a strongn feeling this would be good🎉
@anayatk.007
@anayatk.007 Жыл бұрын
Thank you for providing such fantastic content
@BroskiXP
@BroskiXP 7 ай бұрын
Great explanation, good work
@AnkitkumarDubey-e4r
@AnkitkumarDubey-e4r 8 ай бұрын
VERY NICE EXPLANATION WITH CLARITY . THANKS BHAIYA .
@RAKESHBABUNAIDU
@RAKESHBABUNAIDU Жыл бұрын
Underrated!
@omsudhamsh.h
@omsudhamsh.h 7 ай бұрын
Picture perfect mate! Thanks.
@arnavkukreti2009
@arnavkukreti2009 Жыл бұрын
you explain in a very good manner thank you
@nikoo28
@nikoo28 Жыл бұрын
So nice of you
@neeeelll_
@neeeelll_ Жыл бұрын
Beautifully explained. could u please come up with topic based important questions
@moezzzz9341
@moezzzz9341 Жыл бұрын
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
@sarthakgupta096
@sarthakgupta096 Жыл бұрын
Thankyou sir your way of teaching is amazing
@nikoo28
@nikoo28 Жыл бұрын
It's my pleasure
@dylanjara201
@dylanjara201 Жыл бұрын
Perfect explained. Ty sir!!
@apex095
@apex095 Жыл бұрын
elegant solution. could you please add more questions on variety of topics like Stacks and Queues etc on your website ?
@sumitraj9312
@sumitraj9312 10 ай бұрын
Thank you, ploblem solved. love you 😁
@technicalguy.
@technicalguy. 10 ай бұрын
Thank you soo much ❤❤
@vishwash7416
@vishwash7416 Ай бұрын
GREAT VIDEO
@pranavshekhar2048
@pranavshekhar2048 9 ай бұрын
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.
@sourabhrathore9702
@sourabhrathore9702 2 ай бұрын
Thanks Brother❤
@jerronjones470
@jerronjones470 Жыл бұрын
Yay, someone I can understand
@subee128
@subee128 Жыл бұрын
Thank you
@dineshkm7832
@dineshkm7832 3 ай бұрын
you are super good
@iceyyeah
@iceyyeah 10 ай бұрын
Thanks
@Harrish30
@Harrish30 Жыл бұрын
Perfect!! Thanks
@everyontech2716
@everyontech2716 Жыл бұрын
great explanation
@iiju8212
@iiju8212 Жыл бұрын
Bhai kaafi mast samjhaya.
@nusarathaveliwala
@nusarathaveliwala 11 ай бұрын
why we are not incrementing mid when it is arr[mid] is 2?
@nikoo28
@nikoo28 10 ай бұрын
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 6 ай бұрын
@@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 :)
@RishitMohanty
@RishitMohanty 9 ай бұрын
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 8 ай бұрын
You can, but your interviewer and ask you to solve it without sorting.
@shenth27
@shenth27 8 ай бұрын
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 8 ай бұрын
Its just of solution of 10000000000solutions that u should keep it in your mind
@nikoo28
@nikoo28 8 ай бұрын
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 7 ай бұрын
but interviewer won't be impressed by this approach 😂😂
@porksonline325
@porksonline325 2 ай бұрын
By this approach I am getting errors for 100000 input of integers in array
@bhavukgarg3619
@bhavukgarg3619 Ай бұрын
@shenth27 interviewer can modify the problem saying the attribute color is part of a card class like class Card { int color; String suit; Int value; } Now if you have colection of cards with 3 type of colours you cannot simply count and write in same array as other attributes suit and value can not be copied
@parthsachan1130
@parthsachan1130 3 ай бұрын
OP
@officialdreamplayz
@officialdreamplayz 9 ай бұрын
i used selection sort
@CPS_XI
@CPS_XI 9 ай бұрын
💯❤
@m-bk4um
@m-bk4um Жыл бұрын
good
@SubhajitDas-mt7sn
@SubhajitDas-mt7sn 6 ай бұрын
c++ class Solution { public: void sortColors(vector& nums) { int start = 0; int middle = 0; int end = nums.size() - 1; while (middle
@KarthikC-ju4fx
@KarthikC-ju4fx 6 ай бұрын
@Paadai-Kutti
@Paadai-Kutti Жыл бұрын
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 Жыл бұрын
But complexity?
@nenuanenenuane6645
@nenuanenenuane6645 Жыл бұрын
// 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
@Harshal-dk2kr
@Harshal-dk2kr 3 ай бұрын
bhai whai is vailya
@ankitraj4179
@ankitraj4179 6 ай бұрын
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 11 ай бұрын
Why not just count zeroes and ones and refill the array in place? 😂
@nikoo28
@nikoo28 11 ай бұрын
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 11 ай бұрын
@@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.
@xyzsince
@xyzsince 4 ай бұрын
Too difficult to understand what he says because of his accent.
@nikoo28
@nikoo28 4 ай бұрын
which part were you unable to follow?
@dineshkm7832
@dineshkm7832 3 ай бұрын
Best solution I've seen for this problem. This channel is so underrated.
@vinoths7140
@vinoths7140 Жыл бұрын
Great Explanation, Thank You.
黑天使只对C罗有感觉#short #angel #clown
00:39
Super Beauty team
Рет қаралды 36 МЛН
Quando eu quero Sushi (sem desperdiçar) 🍣
00:26
Los Wagners
Рет қаралды 15 МЛН
What is mathematical thinking actually like?
9:44
Benjamin Keep, PhD, JD
Рет қаралды 24 М.
Leetcode 165 - Compare Version Numbers  - Python Solution
7:35
Data Engineering Studies
Рет қаралды 39
Sort an Array of 0s, 1s & 2s | DNF Sorting Algorithm | Leetcode 75
33:39
The Dome Paradox: A Loophole in Newton's Laws
22:59
Up and Atom
Рет қаралды 1,1 МЛН
黑天使只对C罗有感觉#short #angel #clown
00:39
Super Beauty team
Рет қаралды 36 МЛН