Frequency of the most frequent element | Java | Leetcode | Medium | Sliding Window Moderation

preview_player
Показать описание
The solution provided in the video uses a slight moderation of Sliding window algorithm. Some other solutions may perform search on all the windows either by using linear serach or binary search but these methods may result in TLE.

Рекомендации по теме
Комментарии
Автор

Your explanation is so smooth please make more videos! I would love to go through your channel playlist if I am ever stuck on a question.

shubhambhati
Автор

LeetCode TC 71 fails because of not using long to avoid multiplication overflow
Constraints are violated if long is not used
Take this as a hint, have pasted solution below if you still find trouble figuring it out.

class Solution {
public int maxFrequency(int[] nums, int k) {
Arrays.sort(nums);
int left = 0; int right = 0; // sliding window
long sum = nums[0] ; int ans = 0;
while(right<nums.length){
if(nums[right] * (long) (right - left + 1) <= k + sum){ //
ans = Math.max(ans, right - left + 1);
right++;
if(right<nums.length) sum+= nums[right];
}
else{
sum-=nums[left];
left++;
}
}
return ans;
}
}

multiversemadness
Автор

how do you come up with that logic like how did you think the solution??

hariomverma
Автор

Hey, Before doing sliding window, Need to explain the logic.

iamnoob
Автор

Thank you i was stuck w this problem since 2 days

yusrax
Автор

nums[right] * (right - left + 1) <= k + sum
what is the logic behind this condition?

bikash
Автор

Why are we doing if(right < nums.length) sum += nums[right]; ? which type of case is this applicable for?

samyukta
Автор

Thankyou I understood after seeing this

HemantNavlani
Автор

testcase 72 did not pass . but also thank you for your idea

kaviyans
Автор

kyu koi #hindi m cheeze nhi share krta baiii,

kodewithprince