LeetCode-432:全 O(1) 的数据结构

一、题目描述

在这里插入图片描述

class AllOne {
    unordered_map<string, list<pair<int, unordered_set<string>>>::iterator>
            dict;
    //按first升序
    list<pair<int, unordered_set<string>>> count;
public:
    /** Initialize your data structure here. */
    AllOne() {
        dict.clear();
        count.clear();
    }

    /** Inserts a new key <Key> with value 1. Or increments an existing key by 1. */
    void inc(const string &key) {
        if (dict.find(key) == dict.end()) {
            if (!count.empty() && count.front().first == 1)
                (*(count.begin())).second.insert(key);
            else
                count.insert(count.begin(), {1, {key}});
            dict[key] = count.begin();
        } else {
            auto p = dict.at(key);
            int new_value = (*p).first + 1;
            ++p;
            auto p_next = p;
            --p;
            (*p).second.erase((*p).second.find(key));
            if ((*p).second.empty())
                count.erase(p);
            if (p_next == count.end()) {
                count.push_back({new_value, {key}});
                --p_next;
                dict.at(key) = p_next;
            } else {
                if (new_value == (*p_next).first) {
                    (*p_next).second.insert(key);
                    dict.at(key) = p_next;
                } else {
                    count.insert(p_next, {new_value, {key}});
                    --p_next;
                    dict.at(key) = p_next;
                }
            }
        }
    }

    /** Decrements an existing key by 1. If Key's value is 1, remove it from the data structure. */
    void dec(const string &key) {
        if (dict.find(key) == dict.end())
            return;
        else {
            auto p = dict.at(key);
            if ((*p).first == 1) {
                dict.erase(dict.find(key));
                (*p).second.erase((*p).second.find(key));
                if ((*p).second.empty())
                    count.erase(p);
                return;
            }
            int new_value = (*p).first - 1;
            if (p == count.begin()) {
                (*p).second.erase((*p).second.find(key));
                if ((*p).second.empty())
                    count.erase(p);
                count.insert(count.begin(), {new_value, {key}});
                dict.at(key) = count.begin();
                return;
            }
            --p;
            auto p_pre = p;
            ++p;
            (*p).second.erase((*p).second.find(key));
            if ((*p).second.empty())
                count.erase(p);
            if (new_value == (*p_pre).first) {
                (*p_pre).second.insert(key);
                dict.at(key) = p_pre;
            } else {
                ++p_pre;
                count.insert(p_pre, {new_value, {key}});
                --p_pre;
                dict.at(key) = p_pre;
            }
        }
    }

    /** Returns one of the keys with maximal value. */
    string getMaxKey() {
        if (count.empty())
            return "";
        return *(count.back().second.begin());
    }

    /** Returns one of the keys with Minimal value. */
    string getMinKey() {
        if (count.empty())
            return "";
        return *(count.front().second.begin());
    }
};

四、运行结果

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值