Can be repetitive, but I couldn't find exact one.
This is the part of a problem involving Data Structure and Algotrithms: Given an array of size n, find max frequency among the all elements in the each window/subarray of size k.
Ex1. a = {5,5,7,7,7,5} , k = 3
subarrays of size 3:
{5,5,7} => freq[5] = 2, freq[7] = 1, max frequency = 2
{5,7,7} => freq[5] = 1, freq[7] = 2, max frequency = 2
{7,7,7} => freq[7] = 3, max frequency = 3
{7,7,5} => freq[5] = 1, freq[7] = 2, max frequency = 2
answer array = {2,2,3,2}
Ex2: a = {5,5,6,5,5,6,6}, k = 5 , ans = {4, 3, 3}
Ex3 a = {1,2,3,4,5,6} k = 2, ans = {1,1,1,1,1}
My goal is to solve this problem optimally in time complexity < O(N^2). Something like O(N * logN * logN). Not looking for O(N^2) Solution.
Constraints: N <= 10^5 , k <=N and a[i] <= 10^5
My approach: I tried using an unordered map ( to store frequency of each subarray) and used multiset to resolve the problem of maximum after change in freq.
#include <bits/stdc++.h>
using namespace std;
int check(vector<int> &nums, int k)
{
int n=nums.size();
unordered_map<int,int> mp;
multiset<int> st;
for(int i=0;i<n;i++)
{
if( i >= k )
{
int idx_b = i-k;
auto itr2 = st.find(mp[nums[idx_b]]);
if ( itr2!=st.end() ) st.erase(itr2);
mp[nums[idx_b]]--;
st.insert(mp[nums[idx_b]]);
}
int curr = mp[nums[i]];
mp[nums[i]]++;
auto itr1 = st.find(curr);
if(itr1!=st.end())st.erase(itr1);
st.insert(curr+1);
auto mx = st.rbegin();
if(i>=k-1 )
{
cout<<*mx<<endl;
}
}
return 0;
}
int main()
{
vector<int> a { 5,5,6,6,6,7};
check(a,3);
}
In my understanding the time complexity is O( n * 6 * log(n) ) log(n) for insert, find, and erase. But I am suspecting it to be wrong as the soln is failing as TLE.
Use a max heap and a counting map.
Put elements in the heap based on their count.
Adjust counts & update the heap when elements enter/leave the sliding window.
Running time is O(n log k) for n elements and a window of size k.
Ex2: a = {5,5,6,5,5,6,6}, k = 5 , ans = {4, 3, 3}