Frequency of the Most Frequent Element | Binary Search | Sliding Window | META | Leetcode-1838
Vložit
- čas přidán 16. 11. 2023
- Whatsapp Community Link : www.whatsapp.com/channel/0029...
This is the 13th Video on our Sliding Window Playlist
In this video we will try to solve a very good sliding window problem - Frequency of the Most Frequent Element (Leetcode -1838).
We will also solve it using Binary Search. In total there will be 3 different ways we will solve this problem.
I will explain the intuition so easily that you will never forget and start seeing this as cakewalk EASYYY.
We will do live coding after explanation and see if we are able to pass all the test cases.
Problem Name : Frequency of the Most Frequent Element
Company Tags : META
My solutions on Github(C++ & JAVA) : github.com/MAZHARMIK/Intervie...
Leetcode Link : leetcode.com/problems/frequen...
My DP Concepts Playlist : • Roadmap for DP | How t...
My Graph Concepts Playlist : • Graph Concepts & Qns -...
My GitHub Repo for interview preparation : github.com/MAZHARMIK/Intervie...
Subscribe to my channel : / @codestorywithmik
Instagram : / codestorywithmik
Facebook : / 100090524295846
Twitter : / cswithmik
╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
#coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge#leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview#interviewtips #interviewpreparation #interview_ds_algo #hinglish #github #design #data #google #video #instagram #facebook
College ke computer lab me apka lecture dekh raha hu, ha teacher se permission lia hai maja agaya bhaiya ❤❤❤
Made my day ❤️❤️❤️😄😇🙏
Damn 😅. Our lab teachers won't even let us open CZcams. Else I would have done the same 🥲
@@codestorywithMIK ❤️❤️
@@wearevacationuncoverers 😄
Nicee 😄
Yaar bhaiya kaise itna achhe se soch lete ho aap, Mere liye to kaafi jyada tough hai ye sab. Par itna achha samjhate ho aap majha aa gya
this was indeed a good problem, definately on the medium hard side, i am happy that i learned something new today
So many videos on this topic, but the only one that even remotely helped me understand it completely was this one. Thank you so much for taking all the examples to explain, running each dry run explaining everything in depth. Youve earned yourself a subscriber. Thank you again!!!
You're so welcome! ❤️🙏
It's very mind troubling question but you made it look so easier. I've just subscribed to your channel and you gave solutions to lc daily questions which make them look like a piece of cake😅
The thought process u explained in the start , I was able to think of it by myself
So proud of myself ..........all thanks to your story to code process😇😇
Most welcome 😊
Hi everyone,
I have added a small dry-run correction caption from 38:35 (might reflect after a while). Click on "cc" to display them on screen. Thanks a lot 🙂 (Just needed to sort the array)
bhai learned lot of things from this video. thanks.
Bhaiya you're the best, I suck even on 800 codeforces problems, I've gave up 2 time learning DSA, now I've started from today onwards again just because of your simple explanation made me believe I too can do it. I'm blessed to find your channel.
Thanks a lot bhaiya. Lots of love and support (:
hatsoff to ur fist apprach bro..
I did this ques earlier by learning from another utube channel but he just presented the formula no intuition but the way you explained right from binary search and then sliding window i must say what u wrote in caption-I will explain the intuition so easily that you will never forget and start seeing this as cakewalk EASYYY. u meaned it men!!!! thank u ❤❤
❤️❤️🙏🙏
Sir aapki wajah se me bhi consistent ho gya hu coding me, thanks for making me consistent.
Thank you 🙏😇
Great Explanation 🔥
Ur voice is cute Bhaiya 😊
best of best , keep doing ❤
Very nicely explained ❤
Very Nicely Explained.
Thank u so much...........learnt a lot 💖💖
Incredible content
Again THE best 😊
Your doing a lot of hardwork for us ....❤
Thank you so much for watching 🙏🙏🙏
very nice explanation
👌🏻👌🏻👌🏻That’s pure Art
very nice explnation sir
thank u so much
Loved the explanation 🤌✨️
Amazing ❤❤❤❤
42:14 - There is a mistake
You cannot make all 3 guys in the window as 4. 4 + 4+ 4 = 12 NO.
As you cannot decrement 13 to 4.
Only way is to make every guy as max guy in that Window.
So make all as 13.
13*3 = 39 is what new window sum.
ALGO CORRECTION: To get the MAX - then we to sort it. Then only target = Nums[r] works.
Just a correction. Kindly mention it or pin the comment for others help.
Orelse explaination is awesome. Really the first point - making to a element that is already in array. That doubt I had really on my mind. Why not to any random value.
Cleared it like a smooth knife cut on a butter bar.
Yes yes, let me add that in the caption.
Thank you 🙏😇
I have added a small dry-run correction caption from 38:35 (might reflect after a while). Click on "cc" to display them on screen. Thanks a lot 🙂 (Just needed to sort the array)
Thankyou sir
I love watching your videos. Doing great bhaiya.
Thank you so much 😀
Amazing explanation, I like these in-depth videos, teaches me a lot.
Glad it was helpful! 🙏😇
ur consistency is an inspiration💖💖
Means a lot 😇🙏
if we have array like [1,1,1,4] and k=3
then we can have 2 as max frequency instead of 4.?????
intuition is good, it would be better if you said array is sorted before applying the sliding window in 1st sliding window example.
Yes, my bad I missed that. I have added a small caption for correction. Thank you 😇🙏
I have added a small dry-run correction caption from 38:35 (might reflect after a while). Click on "cc" to display them on screen. Thanks a lot 🙂 (Just needed to sort the array)
1st approach sochane ke liye kitna time laga? do you solved any same question before ? becoz that binary search formulae (count * target) and operation = windows - currsum ; that doesn’t come directly into mind. so give me some advice !!
nice approach first one you did it on first attempt. 💥
Just solved the question then start witching your video. Surprisingly I came up with the 2nd approach. Although it takes 2 long hours for me to build the intuition. But I think your process of your explanation in previous videos helps me a lot. Specially this video: czcams.com/video/JrG4tbq6efg/video.html
Same bro
Bhai jab long long aur int type ke variables ko use karke koi operation/expression perform karte hai to us expression ka result long long hota hai ya int. Please help me out. Great explanation with intuition. Learned new concepts from your video
The solution cleared 57 test cases but gave me tle:
class Solution {
public:
int maxFrequency(vector& nums, int k) {
sort(nums.begin(),nums.end());
int result = 0;
for(int i = nums.size()-1; i != 0; i--){
int j = i-1;
int count = 0;
int temp = k;
while(j >= 0 and temp != 0){
temp = temp - (nums[i]-nums[j]);
if(temp < 0){
break;
}
count++;
j--;
}
result = max(result,count);
}
return result + 1;
}
};
Hey, I am getting runtime error for the following testcast : [40,95,44,37,41,52,07,52,87,64,40,14,41,33,12,34,80,07,80,44,10,9]
7925
Could you help please
instead of prefix sum can't we use inbuilt stl function for computing sum ?? Will it be good or not ?
Accumulate?
@@ru45098 yeahh that one only
Actually accumulate will internally take O(n) time. That’s why prefix sum array is considered.
@@codestorywithMIK ohkk , understood
Solved it only after seeing the sliding window tag 😢
can you explain in java sorting ka sc=logn count karta hai?
if you talking about sc then no , your time complexitey will nlogn
Longest video I have watched
Thank you for watching 🙏😇❤️
What if , we can do both increment or decrement
follow up question aaj ka weekly contest 376 D
Qn-4 czcams.com/video/Iaz-8ZQYUvs/video.htmlsi=4qCFdzYompcrUyhy
Acha agar exactly k elements hota to kaise tweak krenge?
nvm we just change the condition from
@@xiaoshen194 yes. I think in that case == karke hojaega
sir 41:00 dry run of example {1, 8, 13, 4} is not correct becaue the nums is not sorted. 41:52 when right pointer reaches 4, you calculated the number of operations required to change 8, 13 to 4 but 8 and 13 cannot be changed to 4 cause we can only increment the value. Although the code was correct because we already sorted the array before.
Yes yes. Small correction there.
But i hope the explanation was clear.
Thanks Pankaj 😇🙏
explanation is great as always ❤️
Help! I'm unable to pass all test cases (Java)
class Solution {
public int maxFrequency(int[] nums, int k) {
Arrays.sort(nums);
int n = nums.length;
int result = 0;
int i = 0;
int currSum = 0;
for (int j = 0; j < n; j++) {
long target = nums[j];
currSum += nums[j];
if ((j-i + 1) * target - currSum > k) {
currSum -= nums[i];
i++;
}
result = Math.max(result, (j - i + 1));
}
return result;
}
}
god level explaination in very sweet voice👌(●'◡'●)
mzaa aa gya
bhaiya how can i thank you???
When you get placed, give me a small treat of “kulhad waali chai” 😇
Keep working hard 🙌😇
public int maxFrequency (int[]nums,int k){
int ans=0;long sum=0;
Arrays.sort (nums);
for(int i=0,j=0;j
class Solution {
public:
int maxFrequency(vector& nums, int k) {
int n=nums.size();
sort(nums.begin(),nums.end());
vector dif(n-1);
for(int i=0;i
bhaiya mera ye solution dekhna maine greedy approach apply ki h 11th testcase pr fail ho rha h can you tell me why ?
class Solution {
public:
int maxFrequency(vector& nums, int k) {
sort(nums.begin(), nums.end());
int i=0; int j = nums.size()-1;
while(i < j){
if(nums[j] - nums[i]
Can you share the test case
@@codestorywithMIK
Nums =
[9930,9923,9983,9997,9934,9952,9945,9914,9985,9982,9970,9932,9985,9902,9975,9990,9922,9990,9994,9937,9996,9964,9943,9963,9911,9925,9935,9945,9933,9916,9930,9938,10000,9916,9911,9959,9957,9907,9913,9916,9993,9930,9975,9924,9988,9923,9910,9925,9977,9981,9927,9930,9927,9925,9923,9904,9928,9928,9986,9903,9985,9954,9938,9911,9952,9974,9926,9920,9972,9983,9973,9917,9995,9973,9977,9947,9936,9975,9954,9932,9964,9972,9935,9946,9966]
K = 3056
The binary solution was explained really well. However, the sliding window explanation was non intuitive.
Hi there,
Thank you so much for the precious feedback.
I would be glad if you can point out at what point you felt non-intuitive for sliding window ?
I will definitely try my best to clear that out.
Thanks again ❤️❤️🙏🙏😇😇
very bad recursive approch , TLE
class Solution {
public:
int memo[10001];
int dp( vector&nums , int k , int ind , int prev ){
if( ind >= nums.size() or k = 0)
pick = 1 + dp( nums , k - (nums[prev] - nums[ind]), ind + 1 , prev );
notPick = dp( nums , k , ind + 1 , prev);
return max( pick , notPick );
}
int maxFrequency(vector& nums, int k) {
// greedy doesn't work , atleast for now
// lets just tur dp , there might be some subproblems
memset(memo , -1 , sizeof(memo));
sort(nums.begin() , nums.end() , greater());
int ans = INT_MIN;
for( int i = 0 ; i < nums.size() ; i++){
ans = max(ans , dp(nums , k , i , i));
}
return ans ;
}
};
Always a good thing to try multiple approaches