Тёмный

Sort colors | Leetcode #75 

Techdose
Подписаться 171 тыс.
Просмотров 85 тыс.
50% 1

This video explains how to sort an array having 3 elements which are 0, 1 and 2. This video shows 2 techniques which are the brute-force sorting method and the three-pointer technique. If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)

Опубликовано:

 

7 сен 2019

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 241   
@r4riaz
@r4riaz 4 года назад
Thank you, you explained it very well, quick n simple. Others are wasting time for around 20 min.
@techdose4u
@techdose4u 4 года назад
:)
@abhilashpatel3036
@abhilashpatel3036 3 года назад
Same here
@rpw2k23
@rpw2k23 2 года назад
Yes
@pratik2319
@pratik2319 4 года назад
I think you have to use 'else if' instead of if everywhere. Because for this ex 2,0,2,1,1,0 the output will come as 001212 Hope this will help you, or else if I am wrong , Please explain...Thank you
@cloud5887
@cloud5887 4 года назад
You are right.
@akshayshahi296
@akshayshahi296 4 года назад
Right. I got stuck there too. Thanks.
@pratyushbhatt1712
@pratyushbhatt1712 4 года назад
Yup
@preritcreed1526
@preritcreed1526 4 года назад
Yup
@sunnykakrani7830
@sunnykakrani7830 3 года назад
No bro u r perfectly right
@zubairzafar480
@zubairzafar480 Год назад
It was the best video on entire youtube. I love your simplest explanation.
@_overide
@_overide 4 года назад
Very succinctly explained, I remember solving this problem in O(n) but solution was very messy, this is just clean and simple!. Thanks!
@techdose4u
@techdose4u 4 года назад
👍
@shubhamhire9390
@shubhamhire9390 Год назад
Amazing explanation in such a less time👍👌
@jianingzhuang104
@jianingzhuang104 4 года назад
Great video! Concise and clear!
@techdose4u
@techdose4u 4 года назад
Thanks :)
@kshitijshirole2210
@kshitijshirole2210 3 года назад
One idea can be (though not optimal )...iterating through array & calculating occurrences of 0s 1s 2s in 3 variables and placing them (0 1 2 ) accordingly into original array...
@choudharyhimanshu4134
@choudharyhimanshu4134 2 года назад
I have done it like that only, but then I thought that it was not the optimal way and so I came here.
@suraj8092
@suraj8092 6 месяцев назад
That's a bucket sort or count sort approach with 2 - pass solution.
@gigachad6844
@gigachad6844 2 года назад
Count Sort will also do this in O(3N) ie. Linear time O(N) but it's mentioned to do it in one-pass and 3N is actually 3 pass
@arkadeepsen6731
@arkadeepsen6731 3 года назад
You teach really good, I guess it should be else if instead of the if in the latter two conditions.
@sanskrutikapade4238
@sanskrutikapade4238 Год назад
very good, helped me a day before my interview. thanks :)
@kunalkheeva
@kunalkheeva Год назад
simple and precise! thank you.
@shivanshkaushal420
@shivanshkaushal420 3 года назад
sir why in third step we havnot did m++ also? . is it because after swapping arr[h] with arr[m]... the current arr[m] can be 0 or 1 ... if its 1 we'll move forward else if its 0 we'll have to swap it with arr[l]; ??
@sagarbora7768
@sagarbora7768 3 года назад
Thanks found your explanation intutive !
@techdose4u
@techdose4u 3 года назад
Nice :)
@mananvarma5944
@mananvarma5944 3 года назад
Another (simpler) approach would be counting zeros and ones. Then mutating the array accordingly, it'll also take O(n) time & constant space.
@techdose4u
@techdose4u 3 года назад
Yes right.
@mananvarma5944
@mananvarma5944 3 года назад
@@techdose4u But in a face to face round the interviewer might ask to optimize it further in a single pass as this solution takes 2 passes. In that case this Dutch Flag algorithm is the way to go.
@poojaupadhyay6364
@poojaupadhyay6364 3 года назад
this code does not run for multiple test cases on geeksforgeeks platform.
@techdose4u
@techdose4u 3 года назад
Maybe the constraints are changed or else try to tweak the code a little in if else statements.
@hemanthar8363
@hemanthar8363 2 года назад
Nicely explained brother!!
@RajKumar-qv7ci
@RajKumar-qv7ci 4 года назад
Nice explaination. We can solve this just counting number of 1's,2's and 0's .
@techdose4u
@techdose4u 4 года назад
Yes correct.
@ajaychinni3148
@ajaychinni3148 4 года назад
yes we can, but u will have to traverse the array two time once for counting next for replacing them
@Voice_Of_Thoughts
@Voice_Of_Thoughts 4 года назад
@@ajaychinni3148 yah
@neghatnazir1668
@neghatnazir1668 4 года назад
@@ajaychinni3148 nope we can maintain three variables count0 count1 count 2 and treverse array whenever 0 occurs increment count0 similarly for count1 and count2 and thn print ....O(N)
@ssahoo1985
@ssahoo1985 4 года назад
Actually thought that will be still O(N) time but space will not be linear. That catch is it should be linear in space too.
@adityaanand4749
@adityaanand4749 7 месяцев назад
Nice straight forward explanation.
@saisriangajala8399
@saisriangajala8399 3 года назад
I thought of this solution but missed m
@SomnathDas-fg2qc
@SomnathDas-fg2qc 4 года назад
sir can we use this technique whenever we required sorting in a problem,suppose there list of no in aaray,we have to sort it,then by using direct sorting can we use this process
@techdose4u
@techdose4u 4 года назад
No you cannot. This is a very specific technique. Just see the problem and try to think of algo accordingly. Don't take any technique for granted to always work.
@lalitkumarnaidu1
@lalitkumarnaidu1 4 года назад
Thanks for the video, there are some cases which won't work as the terminating conditions are incorrect. Within the loop, we are incrementing pointers which may go out of bounds or may lead to inconsistent results (as Pratik24 mentioned), below will work. while(m
@alstrarame3387
@alstrarame3387 4 года назад
It would be also be Okay if we add else if insted if's
@shadanzahid4594
@shadanzahid4594 4 года назад
I think we don't need to swap if we are getting 1 because if we fix two element i.e 0 and 2 then 1 will be automatically fixed in between , so we can reduce work
@techdose4u
@techdose4u 4 года назад
Yes correct.
@shaswatsingh
@shaswatsingh 4 года назад
WHICH SOFTWARE DID YOU USE
@patilsoham
@patilsoham 4 года назад
Nice explanation...It helped alot. Thank you
@techdose4u
@techdose4u 4 года назад
Welcome :)
@nilaysheth3283
@nilaysheth3283 3 года назад
Can we achieve the solution using low = 0 , mid = n-1 and high = n -1 ?
@dineshrajendran7128
@dineshrajendran7128 2 года назад
Thanks! Your solutions are really well explained and simple to understand.
@techdose4u
@techdose4u 2 года назад
Welcome 😊
@harifrahman8054
@harifrahman8054 4 года назад
Nice solution :) keep going
@techdose4u
@techdose4u 4 года назад
Thanks :)
@VipinChaudhary14119
@VipinChaudhary14119 4 года назад
Very nicely explained . good job
@techdose4u
@techdose4u 4 года назад
Thanks :)
@syno.nymous01
@syno.nymous01 3 месяца назад
The easiest explanation I came across.
@jaydeepmahajan6598
@jaydeepmahajan6598 4 года назад
Hey , but you need to explain , how this idea comes into your mind , like how you analyse the problem and find this approach ?
@techdose4u
@techdose4u 4 года назад
My explanation style was slightly different earlier but later I focussed more on intuition. So maybe you might different kind of approaches to my explanation from an old video compared to the ones I explained in past 4 months. But still I hope you can understand why we took the approach 😅 I evolved my style over time.
@babbarutkarsh7770
@babbarutkarsh7770 4 года назад
@@techdose4u yes this video does'nt seems like TECH DOSE's its more of a gfg kind. We now expect more than just to explain code.
@Shailswap14
@Shailswap14 3 года назад
It is a typical Dutch National Flag Algo problem
@sehejwahla5437
@sehejwahla5437 3 года назад
@@babbarutkarsh7770 Bhai mat dekh fir. He is helping us for free and u have the guts to point out his mistakes !! Don't be a choosing beggar
@varsha9094
@varsha9094 3 года назад
@@sehejwahla5437 have you ever heard of 'constructive criticism' ? He is pointing out mistake to improve quality right!!!...
@user-oy4kf5wr8l
@user-oy4kf5wr8l 3 года назад
Thank you buddy! u made it very clear ! we love u !
@techdose4u
@techdose4u 3 года назад
Welcome ❤️
@user-oy4kf5wr8l
@user-oy4kf5wr8l 3 года назад
​@@techdose4u A question plz Tech dose. There r so many algo, i really don't know how is it possible for me to remember all of these, for example, this video. why m++ when arr[m] == 0 and 1... and also for other questions..... could u plz give me a little suggestion plz? super struggling here....ToT
@techdose4u
@techdose4u 3 года назад
This is not really an algo but just a hack :P You can do using counting the element frequencies :) You will get these ideas once have a lot of practice under your belt 😁 Ping me for any guidance on LinkedIn or Whatsapp or Instagram.
@user-oy4kf5wr8l
@user-oy4kf5wr8l 3 года назад
@@techdose4u Whatsapp number plz !!! thank you ToT
@techdose4u
@techdose4u 3 года назад
@@user-oy4kf5wr8l +91 8918633037
@nishantsharma3100
@nishantsharma3100 4 года назад
nice and fast explanation . liked it bhai.
@techdose4u
@techdose4u 4 года назад
Thanks :)
@DonTaldo
@DonTaldo 3 года назад
Nice explanation! Just a quick note, I think the code should be if( ... ) else if ( ... ) else if ( ... ) instead the if (...) if (...) if (...). I'm doing the "Sort Colors" problem from leetcode and I needed to change that
@iaashu98x
@iaashu98x 2 года назад
if you want to decrease the runtime, don't put 'else if(a[m]==1)' instead put this case in 'else block'. I guess, he was trying to keep the runtime low that why he put only if block but that was incorrect.
@amankumarjha9438
@amankumarjha9438 3 года назад
Very nice explanation sir.
@666Imaginative
@666Imaginative 2 года назад
you give the best explanation FR
@AnasMourad
@AnasMourad 3 года назад
What software do you use?
@shubhamsonal5871
@shubhamsonal5871 4 года назад
Awesome
@karanhemdev5325
@karanhemdev5325 4 года назад
if you want a solution with complexity O(n) then you can also do like this... 1st find all 2's and put it in stack then find all 1's and put it in stack then find all 0's and put it in stack then pop all elements... your output is sorted... here we got (3n) traversal hence equivalent to O(n)... yeah but yours O(n) is better than mine O(n)
@techdose4u
@techdose4u 4 года назад
Yes... And also your technique is taking O(3n) extra space. But the technique in video is for O(1) space.
@sivaganesh4489
@sivaganesh4489 4 года назад
Another apporach is simply count 0's 1's and print it. For 2's len-0's-1's but the solution in video was algorithmic
@nithinkumar9226
@nithinkumar9226 2 года назад
Nice explanation sir Tq ❤
@pabloarkadiusz4687
@pabloarkadiusz4687 3 года назад
This can be solved also with counting sort. And it can also be done in place just using 3 variables instead of array as usually
@zah1d_ali
@zah1d_ali 3 года назад
but than time complexity will be o(n) + o(n) = 2o(n) but using this algo time complexity is just o(n)
@AdrianGonzalezBlogs
@AdrianGonzalezBlogs 3 года назад
@@zah1d_ali O(2n) is still the same as O(n), anyways, both are good solutions
@sangramkapre
@sangramkapre 4 года назад
Why not simply count 0s, 1s and 2s and then fill the array accordingly? I guess it will take two passes as opposed to one so the solution mentioned in the video is more efficient (though might not be scalable when instead of 2s, we have a higher max limit).
@techdose4u
@techdose4u 4 года назад
Yea, but making use of the constraints is a good thing :)
@sehejwahla5437
@sehejwahla5437 3 года назад
Thanks bro !! Ur punjabi fan
@techdose4u
@techdose4u 3 года назад
Welcome paji
@sambeetnayak1002
@sambeetnayak1002 3 года назад
At a time only one condition should be executed, so need to use else if / switch cases. Btw explanation was good.
@bhanupratapsingh8982
@bhanupratapsingh8982 3 года назад
very well explained😁
@techdose4u
@techdose4u 3 года назад
Thanks
@dmsohel1335
@dmsohel1335 4 года назад
this is better with no confusion Keep three counter c0 to count 0s, c1 to count 1s and c2 to count 2s Traverse through the array and increase the count of c0 is the element is 0,increase the count of c1 is the element is 1 and increase the count of c2 is the element is 2 Now again traverse the array and replace first c0 elements with 0, next c1 elements with 1 and next c2 elements with 2.
@techdose4u
@techdose4u 4 года назад
👍
@TheArbaaz-rn2tq
@TheArbaaz-rn2tq 4 года назад
What is the difference if we write len(nums) or len(nums)-1? In 2nd it indicates starting index is 0 then what about len(nums) ?
@techdose4u
@techdose4u 4 года назад
If you wanna access array index then we use len(nums)-1. If you do len(nums) then you need to explicitly subtract. Anyone is good and works fine :)
@akankshakanjolia2969
@akankshakanjolia2969 3 года назад
Awesome explanation
@techdose4u
@techdose4u 3 года назад
Thanks :)
@myriad1703
@myriad1703 Год назад
Thanks for the explanation buddy
@techdose4u
@techdose4u Год назад
No problem 👍
@pryansh_
@pryansh_ 3 года назад
So well explained 😄😄
@techdose4u
@techdose4u 3 года назад
Thanks
@theofficialgauravsharma8731
@theofficialgauravsharma8731 3 года назад
Awesome!
@techdose4u
@techdose4u 3 года назад
Thanks :)
@aznstride4325
@aznstride4325 2 года назад
Counting sort can also solve this in O(n) since range of nums[i] is constant :)
@shubhamraj25
@shubhamraj25 Год назад
yeah but that will take two passes and this algo does it only in single pass :)
@ZalTech
@ZalTech 4 года назад
If we knows that their is only 0,1,2 element. then why not just count frq. Of those 3 and fill the array it's also takes O(n) time and O(1) space.
@techdose4u
@techdose4u 4 года назад
But that will take 2 pass of array. You might be required to solve this in one pass. Like it's required on leetcode.
@Dizzydizzydizzy7
@Dizzydizzydizzy7 3 года назад
@@techdose4u thank you so so much!! This is really a smart solution!
@vishalmane5353
@vishalmane5353 3 года назад
there are some changes in the solution correct solution : if(a[m] ==2) --h; in video : if(a[m] ==2) h--;
@chetanshrivastava3762
@chetanshrivastava3762 3 года назад
Very Nice explanation sir..
@techdose4u
@techdose4u 3 года назад
Thanks :)
@nknidhi321
@nknidhi321 3 года назад
Awesome ❤️
@techdose4u
@techdose4u 3 года назад
Thanks
@CodeSuccessChronicle
@CodeSuccessChronicle 3 года назад
perfect 👌
@techdose4u
@techdose4u 3 года назад
Thanks :)
@ankitupadhyay918
@ankitupadhyay918 2 года назад
Op explanation 🤯🤯🤯🤯😀
@techdose4u
@techdose4u 2 года назад
Thanks :)
@ShreyaSingh-vr9qi
@ShreyaSingh-vr9qi 4 года назад
Awesome explaination, hey you have done a lot of good interview preparation videos. Its a reuqest can u make a repository type something where you can put your videos topicwise ( Graph, Tree,...) . It will help during preparing on a topic to check all imoprtant problems on that topic..
@techdose4u
@techdose4u 4 года назад
I am currently working on my own website. It will be up in around a couple of months. You can find everything well organized there :)
@karann6010
@karann6010 4 года назад
@@techdose4u waiting
@ritishdhiman26
@ritishdhiman26 4 года назад
@@techdose4u You are doing really an amazing work :) Thanks a lot.
@moulikasunesh2834
@moulikasunesh2834 3 года назад
@@techdose4u is the website ready? would love to take a look
@techdose4u
@techdose4u 3 года назад
@@moulikasunesh2834 it's ready. Currently organizing the content.
@saruncse85
@saruncse85 3 года назад
Nice Explanation.
@techdose4u
@techdose4u 3 года назад
Thanks
@tauros5651
@tauros5651 3 года назад
Thanks a lot
@paragroy5359
@paragroy5359 3 года назад
Nice explanation sir.......
@techdose4u
@techdose4u 3 года назад
Thanks
@philosphize
@philosphize Год назад
Thanks for sharing
@ravishankarsingh7512
@ravishankarsingh7512 4 года назад
Why you didn't run the code in java...and where I can get code
@techdose4u
@techdose4u 4 года назад
I can only do one language. The main focus is to explain the logic. You can get code on internet or geeksforgeeks. See comments as well.
@chanpreetsingh007
@chanpreetsingh007 3 года назад
Just keep count of number of 0,1 and 2...and fill the array with 0 ,1 and 2 Respectively
@saiavinashduddupudi8975
@saiavinashduddupudi8975 Год назад
well... thats a 2 pass algo
@Margdarshak_1
@Margdarshak_1 3 года назад
We can also use hashing
@techdose4u
@techdose4u 3 года назад
👍🏼
@harshpatel1385
@harshpatel1385 4 года назад
nice explanation
@techdose4u
@techdose4u 4 года назад
Thanks :)
@ManojKumar-hj7fh
@ManojKumar-hj7fh 4 года назад
How do you come up with approaches, is it because of practice ?
@techdose4u
@techdose4u 4 года назад
Yea.
@DhavalBirade
@DhavalBirade 3 года назад
The same code is written in gfg but nice explanation bro..
@techdose4u
@techdose4u 3 года назад
@@DhavalBirade yep
@premhulikoppe1470
@premhulikoppe1470 2 года назад
short and effective one
@nandhagopalcs5608
@nandhagopalcs5608 3 года назад
Really helpful
@techdose4u
@techdose4u 3 года назад
Thanks
@rishurana9655
@rishurana9655 3 года назад
what is the intuition behind this?
@hassaanistic
@hassaanistic Год назад
love youuu broo
@mwshubham
@mwshubham 3 года назад
Leetcode 75 Sort colors
@techdose4u
@techdose4u 3 года назад
Great man 👌
@gsujithkumar1417
@gsujithkumar1417 3 года назад
Maintaining count of 0,1,2's and putting that many back in the array is far simpler
@vikasrai4915
@vikasrai4915 4 года назад
how can we modify it if we want a stable sort?
@techdose4u
@techdose4u 4 года назад
To maintain order, one simple way is to copy and perform operation on a different array which will take O(N) time and I know you might have thought of this.
@vikasrai4915
@vikasrai4915 4 года назад
please clarify your approach. One approach may be to avoid swapping if both the elements are same and update indexes accordingly.
@AruneshSrivastava
@AruneshSrivastava 4 года назад
nice explanation .. but it's not in-place .. the ordering of 2's are changed ... does anybody know any in-place way of doing it .
@techdose4u
@techdose4u 4 года назад
Inplace means you don't use any extra space but manipulate the words or elements within its own space. I think you are confusing with something else. Read this: en.m.wikipedia.org/wiki/In-place_algorithm
@AruneshSrivastava
@AruneshSrivastava 4 года назад
yeah , you are right .. what i wanted to say ..that it's not stable .. i mean the ordering differs from the original array .
@BarkaDog
@BarkaDog 4 года назад
@@AruneshSrivastava of course the ordering will change you are sorting it lol
@yashnarang637
@yashnarang637 3 года назад
i have a same solution with same approach but a test case of size 65754 gives an error in gfg idk why and the solution is working fine for the element of small array
@rosonerri-faithful
@rosonerri-faithful 3 года назад
yes bro, mine too. i dont understand whats the problem since it is returning 00000000000000.......like gfgs output
@yashnarang637
@yashnarang637 3 года назад
Can anyone answer our question
@rakeshjaiswal2790
@rakeshjaiswal2790 2 года назад
This will not pass all the test cases - for eg. 1,0,1,2,2,1. as you are just incrementing when its 1, the resulting array will not be correctly sorted.
@rahuldey5564
@rahuldey5564 28 дней назад
what is this algorithm called?
@techdose4u
@techdose4u 28 дней назад
dutch national flag maybe :)
@YashM1234
@YashM1234 2 года назад
why don’t we just count 0s ,1s and 2s.. Generate new arr and return O(2N)
@techdose4u
@techdose4u 2 года назад
Doing it inplace and one traversal :)
@NarendraKumar-by6vv
@NarendraKumar-by6vv 6 месяцев назад
ALL THREE if statement doesn't work beacause 3 if condition can be true simultaneously.
@AnkitMishra-mz4xt
@AnkitMishra-mz4xt 4 года назад
USE ELSE IF , other wise the output will be wrong , for such like cases 2 2 1 1 2 1 0 0 1 2 2
@dhanushiyer1241
@dhanushiyer1241 3 года назад
y is that happening can u explain
@ssahoo1985
@ssahoo1985 4 года назад
This solution will not work if we do not have 1 in the list, because m will never increased.
@manisherukulla1779
@manisherukulla1779 3 года назад
But the question itself said array of 0s 1s and 2s
@643kanavguleria9
@643kanavguleria9 3 года назад
i think in while condition the condition should be while(mid while(mid
@ojasvichaudhary8724
@ojasvichaudhary8724 3 года назад
this condition is right mid
@hirakmondal6174
@hirakmondal6174 3 года назад
Why not simply count the number of 0's 1's and 2's and then fill the array?
@techdose4u
@techdose4u 3 года назад
Just a 1 traversal thing ;)
@hirakmondal6174
@hirakmondal6174 3 года назад
@@techdose4u ok XD
@surendharv795
@surendharv795 Год назад
In that way , ur solution will not follow the condition 'single pass'
@user-fq1bk8co5r
@user-fq1bk8co5r Год назад
not working for 2,0,1
@MMNayem-dq4kd
@MMNayem-dq4kd Год назад
Thanks
@utkarshgupta2909
@utkarshgupta2909 3 года назад
Why not just count the 0s 1 2s and place them in array?
@techdose4u
@techdose4u 3 года назад
You can do that too in 2 traversals.
@utkarshgupta2909
@utkarshgupta2909 3 года назад
@@techdose4u yeah.. so this approach was more related to 1 traversal.. got it..
@techdose4u
@techdose4u 3 года назад
@@utkarshgupta2909 yea. Just for interview sake 😅
@hari8568
@hari8568 4 года назад
Much easier solution-Make a count array with elements (0,0,0) where 0th index is 0 count 1st index is 1st count 2nd index 2nd count.Traverse the given array in question once and keep updating the 0 array then rewrite the question array with appropriate counts of 0,1,2
@techdose4u
@techdose4u 4 года назад
This is using 2 traversals. You could have done in 1 traversal by following the swapping method.
@llawliet1750
@llawliet1750 Год назад
the interviewer will kick you out of the interview if you say this
@hari8568
@hari8568 Год назад
@@llawliet1750 it's a solution not the best but ok ,O(2n) approximates to O(n)
@neerajmahapatra5239
@neerajmahapatra5239 3 года назад
Understood.
@neghatnazir1668
@neghatnazir1668 4 года назад
it can b solved using multiset in logn time
@techdose4u
@techdose4u 4 года назад
How? I don't think you can solve below O(N).
@neghatnazir1668
@neghatnazir1668 4 года назад
@@techdose4u i solved this int n; cin>>n; int a[n]; multiset s; for(int i=0;i>a[i]; s.insert(a[i]); } for(auto it=s.begin();it!=s.end();it++) { cout
@killadasatyaaditya5960
@killadasatyaaditya5960 3 года назад
@@neghatnazir1668 The question is to sort, not to print the sort form of given array. You have to modify the array
@shubhamsingh4701
@shubhamsingh4701 2 года назад
instead of a[m]==2 is should be else{} other wise some case will fail for 2 2 1 1 2 1 0 0 1 2 2 and many more
@Voice_Of_Thoughts
@Voice_Of_Thoughts 4 года назад
class Solution { public void sortColors(int[] nums) { int i,j,k; int len=nums.length; i=0; j=0; k=len-1; while(j
@ahmedatef4171
@ahmedatef4171 3 года назад
send this code please
@aksharkashyap5872
@aksharkashyap5872 4 года назад
//those who are getting any problem with the mentioned code on video can run this code: int left = 0, right = a.length -1; for(int i=0;i
@techdose4u
@techdose4u 4 года назад
👍
@AnkitSingh-zj2uc
@AnkitSingh-zj2uc 4 года назад
Dutch algorithm
@techdose4u
@techdose4u 4 года назад
Correct :)
@techconvos
@techconvos 4 года назад
this explaination coould be more better and clear, next time while making video ensure that you will work on that
@marriagedance8657
@marriagedance8657 4 года назад
U should have explained the intuition. Instead, You just dry run the solution. Not much beneficial.
@techdose4u
@techdose4u 4 года назад
The intuition is already clear from the dry run. Since you have just 3 different values, so you can sort them like a Dutch flag. The intuition was already clear i guess. Now the tricky thing was to sort in just O(N) instead of O(NlogN). So we used the 3 pointer technique which segregates those values. I hope it's clear now :)
@spetsnaz_2
@spetsnaz_2 4 года назад
Can we name this method as Indian Flag Method??
@techdose4u
@techdose4u 4 года назад
Yes you can :P
@sivaganesh4489
@sivaganesh4489 4 года назад
Good idea
@ankitraj4179
@ankitraj4179 Месяц назад
Java Code (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-- ; } } } }
@mathan3192
@mathan3192 4 года назад
two line using Java lib function , but not O(n) :| Arrays.sort(arr); System.out.println(Arrays.toString(arr).replaceAll("\\]","").replaceAll(",",""). replaceAll("\\[",""));
Далее
Find equilibrium point in an array
10:32
Просмотров 54 тыс.
Remove K digits | Build lowest number | Leetcode #402
15:30
Search in rotated sorted array | Leetcode #33
13:52
Просмотров 83 тыс.
Contiguous array | Leetcode #525
13:12
Просмотров 52 тыс.
LeetCode Sort Colors Solution Explained - Java
7:40
Просмотров 32 тыс.
Rearrange array alternately
10:37
Просмотров 73 тыс.
Counting inversions in an array
19:03
Просмотров 90 тыс.
Longest common substring | Dynamic programming
20:47
Просмотров 66 тыс.
Merge sort algorithm
18:20
Просмотров 2,2 млн
Kadanes algorithm | Longest sum contiguous subarray
7:51
Sort Colors | LeetCode 75 | C++, Java, Python
18:50
Просмотров 6 тыс.