Counting Sort - O(n) Solution to Sort Colors - Leetcode 75

preview_player
Показать описание

Please check my playlists for free DSA problem solutions:

Best Courses for Analytics:
---------------------------------------------------------------------------------------------------------

Best Courses for Programming:
---------------------------------------------------------------------------------------------------------

Best Courses for Machine Learning:
---------------------------------------------------------------------------------------------------------

Best Courses for Statistics:
---------------------------------------------------------------------------------------------------------

Best Courses for Big Data:
---------------------------------------------------------------------------------------------------------

More Courses:
---------------------------------------------------------------------------------------------------------

Full Disclosure:
Please note that I may earn a commission for purchases made at the above sites! I strongly believe in the material provided; I only recommend what I truly think is great. If you do choose to make purchases through these links; thank you for supporting the channel, it helps me make more free content like this!
Рекомендации по теме
Комментарии
Автор

Master Data Structures & Algorithms For FREE at AlgoMap.io!

GregHogg
Автор

I am currently preparing for a Google Phone Interview, and I accidentally scrolled across your video, which is very helpful

yuuhiiujnj
Автор

dutch national flag algorithm is the optimal and expected way with N runtime 1 space

janardannn
Автор

You don't need to store the red and just immeduatly add it to array, because it is going to be at the start either way. You could also do same from white, because you know how large the array is

AnAbsolutelyRandomGuy
Автор

this is so basic omg, i instantly thought of this upon seeing this

zrocool
Автор

Hello Mr.Greg, this ans is a 2 pass solution, but Neetcode explained a one pass answer but it's very complicated.. Mr.Greg if possible just try to explain that version too.. it might improve our way of approaching such problems. (Your ans is GOAT, I just want to learn the other way too)

Pv-vcyu
Автор

It needs O(n) of additional memory to be sustainable

СавелийИсаев-ък
Автор

What if interviewer expects me to solve using dnf sort 😂

technosujeet
Автор

can someone provide the full code thats written in the video?

wraitth
Автор

Using three pointers we can solve this problem which is the most optimal solution

santhoshm
Автор

Can someone tell if this approach is correct or not?
I think we can do it with 2 pointers approach. If there are only 3 integers that we have to account for theek we can have a starting index and a last index. So, 0's are supposed to be swapped to the starting index and the 2's are supposed to be swapped with the last index and then increment the starting pointer and decrement the ending pointer. If we just sort 0 and 2, 1 will automatically be sorted.

vaibhavsharma
Автор

If you need to go over the array to count colors, isn’t it O(n)?

yonatannethanel
Автор

So you telling me I only need 32 or 64 extra bits for every single number. Better hope the client doesn't put in a number in the billions 😂

valrina
Автор

So it's the same as using HashMap, only with hashmap you get a universal solution any number of different colors

TheIvasyl
Автор

I wouldn't call the space complexity constant simply because the size is known. O(1) implies that it would be the same even if the size changes. If you increase the number of colors, the space required would increase linearly, so O(m) where m is the number of colors.

DCTOR-ZED
Автор

why cant you just move 2s to the end of the list and 0s the beginning

jjophoven
Автор

bro your voice sounds like you're going through puberty

maixmm