您的位置:首页 > 其它

432. All O`one Data Structure

2017-02-13 13:44 453 查看
Implement a data structure supporting the following operations:

Inc(Key) - Inserts a new key with value 1. Or increments an existing key by 1. Key is guaranteed to be a
non-empty string.
Dec(Key) - If Key's value is 1, remove it from the data structure. Otherwise decrements an existing key by 1. If the key does not exist, this function does nothing. Key is guaranteed to be a
non-empty string.
GetMaxKey() - Returns one of the keys with maximal value. If no element exists, return an empty string
""
.
GetMinKey() - Returns one of the keys with minimal value. If no element exists, return an empty string
""
.
Challenge: Perform all these in O(1) time complexity.

Subscribe to see which companies asked this question.

实现一个所由操作都是O(1)时间复杂度的数据结构,操作包括增加key对应的计数值,减少key对应的计数值,返回计数值最大的key和计数值最小的key。和460.LFU Cache的实现类似,用一个链表保存计数值到拥有此计数值的key的集合的映射,另外为了快速的查找,用一个unordered_map保存key到key在上述链表中的位置的映射。然后链表的更新套路和460题的实现类似,具体实现看代码吧。

代码:

class AllOne
{
using set_t = unordered_set<string>;
using list_t = list<pair<int, set_t> >;
using map_t = unordered_map<string, list_t::iterator>;
public:
AllOne()
{
}

void inc(string key)
{
auto iter = map.find(key);
if(iter != map.end())
{
auto list_node = iter->second;
list_node->second.erase(key);
if(next(list_node) == ls.end() || next(list_node)->first != list_node->first + 1)
{
ls.insert(next(list_node), make_pair(list_node->first + 1, set_t({key})));
}
else
{
next(list_node)->second.insert(key);
}
map[key] = next(list_node);

if(list_node->second.empty()) ls.erase(list_node);
}
else
{
if(ls.empty() || ls.front().first != 1)
{
ls.push_front(make_pair(1, set_t({key})));
}
else
{
ls.front().second.insert(key);
}
map[key] = ls.begin();
}
}

void dec(string key)
{
auto iter = map.find(key);
if(iter != map.end())
{
auto list_node = iter->second;
list_node->second.erase(key);
if(list_node->first == 1)
{
map.erase(iter);
}
else
{
if(list_node == ls.begin() || prev(list_node)->first != list_node->first - 1)
{
ls.insert(list_node, make_pair(list_node->first - 1, set_t({key})));
}
else
{
prev(list_node)->second.insert(key);
}
map[key] = prev(list_node);
}
if(list_node->second.empty()) ls.erase(list_node);
}
}

string getMaxKey()
{
if(ls.empty()) return string("");
return *(ls.back().second.begin());
}

string getMinKey()
{
if(ls.empty()) return string("");
return *(ls.front().second.begin());
}

void show()
{
for(auto l:ls)
{
printf("count(%d):\t", l.first);
for(auto p:l.second)
{
printf("%s\t", p.c_str());
}
printf("\n");
}
}

private:
list_t ls;
map_t map;

};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  design