We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Here is my solution. Not very elegant but still manages to pass all the tests.
vector<int>freqQuery(vector<vector<int>>queries){//initialize two maps where ://freq has for key the frequence z and the value is the datas having this frequency//values has for key the data inserted and the key is it's frequencystd::map<int,int>freq;std::map<int,int>datas;//vector of the final outputstd::vector<int>output;for(auto&q:queries){switch(q[0]){case1://incrementing the frequency of the datadatas[q[1]]++;//increment the number of datas having this frequencyfreq[datas[q[1]]]++;//decrement the number of datas having the old frequency of the treated data freq[datas[q[1]]-1]--;//we cannot allow to have a negative number in our valuesif(freq[datas[q[1]-1]]<0){freq[datas[q[1]-1]]=0;}break;case2://case 2 is like case 1 but the incrementation and decrementation will be reverseddatas[q[1]]--;freq[datas[q[1]]]++;freq[datas[q[1]]+1]--;if(freq[datas[q[1]]+1]<0){freq[datas[q[1]]+1]=0;}if(datas[q[1]]<0){datas[q[1]]=0;}break;case3://check whether this frequency exists in our map and at least one data have this frequencyif(freq.find(q[1])!=freq.end()&&freq[q[1]]>0){output.push_back(1);}else{output.push_back(0);}break;}}returnoutput;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Frequency Queries
You are viewing a single comment's thread. Return to all comments →
Here is my solution. Not very elegant but still manages to pass all the tests.