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

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

Nikhil Lohia

2 года назад

30,477 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@jerronjones470
@jerronjones470 - 15.09.2023 05:33

Yay, someone I can understand

Ответить
@m-bk4um
@m-bk4um - 02.10.2023 15:32

good

Ответить
@RAKESHBABUNAIDU
@RAKESHBABUNAIDU - 13.10.2023 07:27

Underrated!

Ответить
@anayatk.007
@anayatk.007 - 04.12.2023 15:12

Thank you for providing such fantastic content

Ответить
@subee128
@subee128 - 01.01.2024 13:48

Thank you

Ответить
@moezzzz9341
@moezzzz9341 - 01.01.2024 21:41

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 <= end:

if nums[middle] == 0:
nums[start], nums[middle] = nums[middle], nums[start]
start += 1
middle += 1

elif nums[middle] == 1:
middle += 1

elif nums[middle] == 2:
nums[end], nums[middle] = nums[middle], nums[end]
end -= 1

Ответить
@omdongare2005
@omdongare2005 - 06.01.2024 16:29

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 - 06.01.2024 20:47

very good explanation! keep up the good work

Ответить
@hwval-zw4hy
@hwval-zw4hy - 11.01.2024 00:35

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

Ответить
@Azeem_Idrisi
@Azeem_Idrisi - 13.01.2024 12:04

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

Ответить
@shwetayadav4244
@shwetayadav4244 - 26.01.2024 11:19

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

Ответить
@nusarathaveliwala
@nusarathaveliwala - 31.01.2024 19:00

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

Ответить
@erytymyeah
@erytymyeah - 04.02.2024 20:51

Thanks

Ответить
@technicalguy.
@technicalguy. - 16.02.2024 15:24

Thank you soo much ❤❤

Ответить
@sumitraj9312
@sumitraj9312 - 25.02.2024 16:17

Thank you, ploblem solved. love you
😁

Ответить
@officialdreamplayz
@officialdreamplayz - 06.03.2024 12:28

i used selection sort

Ответить
@pranavshekhar2048
@pranavshekhar2048 - 17.03.2024 19:16

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.

Ответить
@CPS_XI
@CPS_XI - 18.03.2024 15:32

💯❤

Ответить
@RishitMohanty
@RishitMohanty - 30.03.2024 18:17

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???

Ответить
@shenth27
@shenth27 - 21.04.2024 06:35

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.

Ответить
@AnkitkumarDubey-e4r
@AnkitkumarDubey-e4r - 21.04.2024 09:09

VERY NICE EXPLANATION WITH CLARITY . THANKS BHAIYA .

Ответить
@BroskiXP
@BroskiXP - 08.05.2024 22:00

Great explanation, good work

Ответить
@omsudhamsh.h
@omsudhamsh.h - 18.05.2024 13:24

Picture perfect mate!
Thanks.

Ответить
@sithutun688
@sithutun688 - 18.05.2024 13:58

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

Ответить
@ankitraj4179
@ankitraj4179 - 12.06.2024 07:17

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<3 ; i++){
count = arr[i] ;
while(count > 0){
nums[k] = i ;
k++ ;
count-- ;
}
}
}
}

Ответить
@SubhajitDas-mt7sn
@SubhajitDas-mt7sn - 12.06.2024 14:06

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

while (middle <= end) {
if (nums[middle] == 0) {
swap(nums[start], nums[middle]);
start++;
middle++;
} else if (nums[middle] == 1) {
middle++;
} else if (nums[middle] == 2) {
swap(nums[middle], nums[end]);
end--;
}
}
}

void swap(int& a, int& b) {
int temp = a;
a = b;
b = temp;
}
};

Ответить
@KarthikC-ju4fx
@KarthikC-ju4fx - 14.06.2024 23:33

Ответить
@unanimous8510
@unanimous8510 - 19.06.2024 07:42

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 - 25.06.2024 07:43

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

Ответить
@jameswalt2199
@jameswalt2199 - 22.07.2024 15:49

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

Ответить
@xyzsince
@xyzsince - 16.08.2024 15:52

Too difficult to understand what he says because of his accent.

Ответить
@Aspiringactor02
@Aspiringactor02 - 20.08.2024 01:37

This is better than strivers i watched him then i watched you had a strongn feeling this would be good🎉

Ответить
@dineshkm7832
@dineshkm7832 - 07.09.2024 18:41

you are super good

Ответить
@dineshkm7832
@dineshkm7832 - 07.09.2024 18:42

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

Ответить
@captainstring3820
@captainstring3820 - 16.09.2024 14:42

really very good explanation. you made it so easy

Ответить
@Harshal-dk2kr
@Harshal-dk2kr - 16.09.2024 23:32

bhai whai is vailya

Ответить
@parthsachan1130
@parthsachan1130 - 17.09.2024 18:54

OP

Ответить
@sourabhrathore9702
@sourabhrathore9702 - 07.10.2024 21:23

Thanks Brother❤

Ответить
@vishwash7416
@vishwash7416 - 21.11.2024 19:08

GREAT VIDEO

Ответить
@naol80
@naol80 - 22.11.2024 17:55

wow. such a simple and wonderful solution

Ответить
@shabanashaik2784
@shabanashaik2784 - 03.12.2024 17:48

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.

Ответить
@themodernmonk7
@themodernmonk7 - 18.12.2024 14:28

Wow wow. I watched tons of video on DNF algoritm, I didn't understood whay they are saying. You explaind it excellently.

Ответить
@LIKHITHA-q9o
@LIKHITHA-q9o - 08.01.2025 15:09

thanks a tonn very clear explanation !! beginner friendly

Ответить
@samarthpawar6906
@samarthpawar6906 - 20.01.2025 09:57

Most recommended Channel for DSA Question Solving.

Ответить
@prithbi777
@prithbi777 - 25.01.2025 16:42

why XOR swapping is not working here ???????????????????????????

Ответить
@muskanagrawal7399
@muskanagrawal7399 - 27.01.2025 17:59

Nice video...thanku sir

Ответить
@doc2407
@doc2407 - 12.02.2025 18:03

Thanks Nikhil sir, you really made the concept easy!

Ответить
@premmore2304
@premmore2304 - 23.02.2025 09:32

Why did you make a special function swap ??

Ответить
@SreekanthManga
@SreekanthManga - 02.03.2025 15:44

This is nice explanation unlike others who tried to explain the algorithm with out explaining the logic

Ответить