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.
Use one Map for insertion and deletion of the value and another Map for the Frequency of the occurence.
vector freqQuery(vector> queries) {
vector retVector;
std::unordered_map elemValueMap;
std::unordered_map freqMap;
std::unordered_map::iterator elemValueMapIter;
int nLoopCounter = 0;
std::cout << queries.size() << std::endl;
for (nLoopCounter = 0; nLoopCounter < queries.size(); nLoopCounter++) {
vector tempVector = queries.at(nLoopCounter);
int nValue = tempVector.at(1);
switch (tempVector.at(0)) {
case 1:
// Remove 1 from the freqMap as the freq is moving to next freq
if(freqMap[elemValueMap[nValue]])
freqMap[elemValueMap[nValue]]--;
elemValueMap[nValue]++;
// Add 1 to the freqMap for the new freq
freqMap[elemValueMap[nValue]]++;
break;
case 2:
if(elemValueMap.count(nValue)) {
if(elemValueMap[nValue] > 0) {
// Remove 1 from the freqMap as the freq is moving to prev freq
if(freqMap[elemValueMap[nValue]] > 0)
freqMap[elemValueMap[nValue]]--;
elemValueMap[nValue]--;
// Add 1 to the freqMap for the old reduced freq
freqMap[elemValueMap[nValue]]++;
}
}
break;
case 3:
int nFlag = (0 != freqMap[nValue]) ? 1 : 0;
retVector.push_back(nFlag);
break;
}
}
return (retVector);
}
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 →
Use one Map for insertion and deletion of the value and another Map for the Frequency of the occurence.
vector freqQuery(vector> queries) { vector retVector; std::unordered_map elemValueMap; std::unordered_map freqMap; std::unordered_map::iterator elemValueMapIter; int nLoopCounter = 0; std::cout << queries.size() << std::endl; for (nLoopCounter = 0; nLoopCounter < queries.size(); nLoopCounter++) { vector tempVector = queries.at(nLoopCounter); int nValue = tempVector.at(1); switch (tempVector.at(0)) { case 1: // Remove 1 from the freqMap as the freq is moving to next freq if(freqMap[elemValueMap[nValue]]) freqMap[elemValueMap[nValue]]--; elemValueMap[nValue]++;
}