Sort an Array | Leetcode 912 | codestorywithMIK

preview_player
Показать описание
This is the 3rd Video on our Sorting Playlist.
In this video we will try to solve an easy but yet very important Problem "Sort Characters By Frequency" asked by Microsoft.

Problem Name : Sort an Array
Company Tags : Microsoft, Goldman Sachs, Cisco

╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
#coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview #interviewtips
#interviewpreparation #interview_ds_algo #hinglish
Рекомендации по теме
Комментарии
Автор

Nice Approach and Explanation !
I tweaked a bit, Implemented using Vector for storing frequency instead of map...

vector<int> freq;
vector<int> sortArray(vector<int>& nums) {
int maxi = *max_element(begin(nums), end(nums));
int mini = *min_element(begin(nums), end(nums));

if(mini < 0)
mini *= -1;
freq.resize(maxi + mini + 1);

for(int i = 0; i < nums.size(); i++)
{
freq[nums[i] + mini]++;
}

int i = 0;
for(int k = 0; k <= maxi + mini; k++)
{
while(freq[k])
{
nums[i++] = k - mini;
freq[k]--;
}
}
return nums;
}

utpaltripathi
Автор

This was such a refreshing solution to a regular sorting problem statement :) thanks a lot for counting sort knowledge.

krutikadave
Автор

interesting algo first time seeing this

YashSinghal
Автор

this is a new and very good approach, thanks for it
the only issue here is we are using extra space (by means of map) which may not be ok for all interviewers, thus, in this case, I think we need to fallback to mergeSort algo etc

DurgaShiva
Автор

Please continue the graph concept series 😊

shreyasrivastava
Автор

What if we used ordered map. It will be sorted automatically

nileshsinha
Автор

Sir can u make a playlist or video on all the algorithms and tricks used to solve the problem for eg like sweep line algo etc

yuvhrajverma
Автор

Sir, sorting algorithms ka ek playlist banadijiye na

pritishpattnaik
Автор

can you also post solutions for gfg potd daily. Please it's a request.

avinashvishwakarma
Автор

Bhaiya, HeapSort seems the ideal candidate for this problem since the time complexity is O(NlogN) and space complexity is O(1)

drbullah
Автор

Bro can you post HEAP SORT video as I have interview (Please Buddy)

molyoxide
Автор

Can you also make a video on Heap sort bhaiya

souravjoshi
Автор

but this is not possible if the range of values are more and how did you know to use here count sort instead of merge or heap sort

PRUDHVIRAJMACHERLA