Sort Array by Increasing Frequency | Lambda Expression | Hashing | Leetcode DSA series | Hindi
Vložit
- čas přidán 10. 06. 2021
- This is the video under the series of DATA STRUCTURE & ALGORITHM in a HASHING Playlist. We are going to solve the problem "Sort Array by Increasing Frequency" from Leetcode which is solved by using a unordered_map in c++.
Join My Telegram channel for more Updates: telegram.me/helloworldbyprince
Given an array of integers nums, sort the array in increasing order based on the frequency of the values. If multiple values have the same frequency, sort them in decreasing order.
Return the sorted array.
Input: nums = [1,1,2,2,2,3]
Output: [3,1,1,2,2,2]
Explanation: '3' has a frequency of 1, '1' has a frequency of 2, and '2' has a frequency of 3.
----------------------------------------------------------------------------------------
Lambda Expression: www.geeksforgeeks.org/lambda-...
Sort Array by Increasing Frequency: leetcode.com/problems/sort-ar...
code in This Video: github.com/Prince-1501/Hello_...
Pdf in this video: github.com/Prince-1501/Hello_...
----------------------------------------------------------------------------------------
*Follow me *
LinkedIn► / iamprince
Facebook► / helloworldofficials
Instagram► / helloworldbyprince
Twitter► / prince_king_
Telegram► telegram.me/helloworldbyprince
----------------------------------------------------------------------------------------
►Our Playlists on:-
►Competitive Programming: • How to start Competiti...
►C++ Full Course : • L-01 || Introduction a...
►Algorithms: • L-01 || Prefix Sum Arr...
►Data Structure: • Data Structures with C...
------------------------------------------------------------------------
🌟 Please leave a LIKE ❤️ and SUBSCRIBE for more AMAZING content! 🌟
✨ Tags ✨
how to Sort Array by Increasing Frequency
question asked in Google
what is a lambda expression
how to crack online coding test
how to crack Google Interview
off-campus placement
how to learn to code for beginners
Practice Hashing data structure
hashing in data structure
Best Telegram channel for Off-campus Placement drive
hashing in a data structure in Hindi
unordered_map
#hashing #geeksforgeeks #programming
solved on my own without even looking at the solution.........
thanks bhaiya from the bottom of my heart........all credit goes to u
your series literally boosted my confidence
Dil ❤️ se shukriya
Gazab Explanation 🔥🔥🔥
also, you can try hashmap and minheap, which gives same ans with the same complexity
teen account se subscibe kiya hu,
because you really deserve it ..................
Waoo thanks for the support buddy
Very well explained. Thank you bhaiyya
you are the best........masum.
❤️❤️❤️❤️❤️
Similar problem on gfg gives error of time limit exceeded after passing 119 test case out of 240 .
Problem :Sorting element of an array by frequency on gfg
Can you provide other more optimized solution?
Sir bahut dino bad video aaya h
Tree ke videos thoda jaldi jaldi dalna sir
Ekdum sure
WHEN THE DAYS ARE SORROW AND NIGHT ARE BITTER,
THEN A JOURNEY SEEMS LIKE A BURDEN,
DONT LOSS YOUR HOPE , AND KEEP SOME SOAP AS A TOOL TO CLEAN YOUR PATH OF HURDEL.
😎😎 Yes
costum comparator and lambda fn alg h ?
By using 2 ordered maps
class Solution {
public:
vector frequencySort(vector& nums)
{
mapumap;
map map_freq;
vector final_vec;
for(auto a:nums)
{
umap[a]++;//Saves numbers and their frequency
}
for(auto k:umap)
{
map_freq[k.second].push_back(k.first);// map like frequency->(vector containing numbers
//having that frequency)
}
for(auto j:map_freq)
{
for(int i=j.second.size()-1;i>=0;i--)//reversing vector so that it can be sorted in descending order when frequency is same
{
for(int l=0;l
good work
thx bhaiya
thanks bro
Iski time complexity O (log n) kaise hui?
/*Example Comparisons During Sorting
Initial Vector:
nums = {4, 6, 2, 6, 4, 4, 3}
Comparator Function:
sort(nums.begin(), nums.end(), [&](int a, int b){
return umap[a] != umap[b] ? umap[a] < umap[b] : a > b;
});
Comparing 4 and 6:
umap[4] = 3
umap[6] = 2
Since umap[4] (3) is not equal to umap[6] (2), compare based on frequency:
umap[4] < umap[6] evaluates to 3 < 2 which is false.
So, 4 should come after 6.
Second Comparison
Comparing 6 and 2:
umap[6] = 2
umap[2] = 1
Since umap[6] (2) is not equal to umap[2] (1), compare based on frequency:
umap[6] < umap[2] evaluates to 2 < 1 which is false.
So, 6 should come after 2.
Third Comparison
Comparing 2 and 6:
umap[2] = 1
umap[6] = 2
Since umap[2] (1) is not equal to umap[6] (2), compare based on frequency:
umap[2] < umap[6] evaluates to 1 < 2 which is true.
So, 2 should come before 6.
Fourth Comparison
Comparing 6 and 4:
umap[6] = 2
umap[4] = 3
Since umap[6] (2) is not equal to umap[4] (3), compare based on frequency:
umap[6] < umap[4] evaluates to 2 < 3 which is true.
So, 6 should come before 4.
Fifth Comparison
Comparing 4 and 4:
umap[4] = 3
umap[4] = 3
Since umap[4] is equal to umap[4], compare based on value:
4 > 4 evaluates to false.
So, the order remains unchanged.
Final Sorted Vector
After all necessary comparisons(SORT FUNCTION WILL SEE THIS, YOU MY FRIEND DONT CARE OF IT) and swaps, the final sorted vector nums is:
{2, 3, 6, 6, 4, 4, 4}
Summary of a and b in Comparisons
During sorting, a and b are elements from the nums vector that are being compared.
The custom comparator checks:
If the frequencies of a and b are different (umap[a] != umap[b]), it returns true if a's frequency is less than b's frequency.
If the frequencies are the same, it returns true if a is greater than b to ensure that larger elements come first for the same frequency.
*/
In place of lamda function can we use comparator
yess
Bhaiya graph ki series kab ane wali he??
Tree ke baad
Tree ke baad maximum confidence aa jayega
7:22
bhaiya apne bass code kr diya hai...vhi to samjna hai vhi nhi btaye aap
I will request u to please watch hashing playlist from starting buddy
heap sol
unordered_mapumap;
for(auto x:nums)umap[x]++;
priority_queuemaxh;
for(auto x:umap){
maxh.push({-x.second,x.first});//- lga ke daal denge usse lest freq wla max bn jyega aur pop krne ke time seedha result aa jyega
}
vectortemp;
while(!maxh.empty()){
//to print acc to freq
int freq = maxh.top().first;
int element = maxh.top().second;
for(int i=1;i
Amazing yaar
Jun 19 2022, results are out of 6th sem yesterday. Placements are coming. Ask me after 1 year how am i and where am i
Pakaa promise I will ask
@OGBuddhA Ha Bhai kidhar hai?
@@shubhambhatt2704 placed in an international company. Good package 11lpa
17/18 done (11.12.22)
good
bhaiya maine asa socha pr problem aa rha
class Solution {
public:
vector frequencySort(vector& nums) {
vector ans;
unordered_map um;
for(auto x:nums)
{
um[x]++;
}
set s;
for(auto x:um)
{
auto temp=x.second;
s[temp]++;
}
for(auto x:s)
{
ans.push_back(x);
}
return ans;
}
};
kya problem aa rha dry run karo koi ek example lekar samjh me aa jayega ki kya gaklti ho rha hai
how to solve this problem without using lambda function
Yaha ye kaise determine hua ki a is greater than b hi hai???
Maine kaha tha, lambda function me ye maan ke chal rahe hai ki a pahle aayega b se
var frequencySort = function(nums) {
let map = new Map()
for(let i=0;ia[1]-b[1])
// console.log(sortedMap)
let index = 0
for(let i=0;i0){
sortedMap[i][1] = sortedMap[i][1] -1
nums[index++] = sortedMap[i][0]
}
}
return nums
};
solved by own now will watch your tutorial
Also in java??
sure but it will take some time
Bhaiya return wali line ek bar bta do smjh ni ayi Please Please Pleaseeeeeeeeeeeeeeeee.........
kya nhi samjha ussme aap ye batao ek baar
please Hashing starting se dekho aap please
and ek baar dry run v karo mere code ko
its your homework