Skip to content

Commit 90d3424

Browse files
authored
Create solution.cpp
1 parent b30e77f commit 90d3424

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

AllO'oneDataStructure/solution.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
class AllOne {
2+
private:
3+
unordered_map<string, int> strCountMap;
4+
public:
5+
AllOne()
6+
{
7+
8+
}
9+
10+
void inc(string key)
11+
{
12+
strCountMap[key]++;
13+
}
14+
15+
void dec(string key)
16+
{
17+
if (--strCountMap[key] <= 0)
18+
strCountMap.erase(key);
19+
}
20+
21+
string getMaxKey()
22+
{
23+
int maxCount = 0;
24+
string maxCountKey = "";
25+
for (const auto& [key, count] : strCountMap)
26+
{
27+
if (count > maxCount)
28+
{
29+
maxCount = count;
30+
maxCountKey = key;
31+
}
32+
}
33+
34+
return maxCountKey;
35+
}
36+
37+
string getMinKey()
38+
{
39+
int minCount = INT_MAX;
40+
string minCountKey = "";
41+
for (const auto& [key, count] : strCountMap)
42+
{
43+
if (count < minCount)
44+
{
45+
minCount = count;
46+
minCountKey = key;
47+
}
48+
}
49+
50+
return minCountKey;
51+
}
52+
};
53+
54+
/**
55+
* Your AllOne object will be instantiated and called as such:
56+
* AllOne* obj = new AllOne();
57+
* obj->inc(key);
58+
* obj->dec(key);
59+
* string param_3 = obj->getMaxKey();
60+
* string param_4 = obj->getMinKey();
61+
*/

0 commit comments

Comments
 (0)