C++ | Leetcode C++题解之第432题全O(1)的数据结构

ops/2024/9/24 5:59:39/

题目:

题解

class AllOne {list<pair<unordered_set<string>, int>> lst;unordered_map<string, list<pair<unordered_set<string>, int>>::iterator> nodes;public:AllOne() {}void inc(string key) {if (nodes.count(key)) {auto cur = nodes[key], nxt = next(cur);if (nxt == lst.end() || nxt->second > cur->second + 1) {unordered_set<string> s({key});nodes[key] = lst.emplace(nxt, s, cur->second + 1);} else {nxt->first.emplace(key);nodes[key] = nxt;}cur->first.erase(key);if (cur->first.empty()) {lst.erase(cur);}} else { // key 不在链表中if (lst.empty() || lst.begin()->second > 1) {unordered_set<string> s({key});lst.emplace_front(s, 1);} else {lst.begin()->first.emplace(key);}nodes[key] = lst.begin();}}void dec(string key) {auto cur = nodes[key];if (cur->second == 1) { // key 仅出现一次,将其移出 nodesnodes.erase(key);} else {auto pre = prev(cur);if (cur == lst.begin() || pre->second < cur->second - 1) {unordered_set<string> s({key});nodes[key] = lst.emplace(cur, s, cur->second - 1);} else {pre->first.emplace(key);nodes[key] = pre;}}cur->first.erase(key);if (cur->first.empty()) {lst.erase(cur);}}string getMaxKey() {return lst.empty() ? "" : *lst.rbegin()->first.begin();}string getMinKey() {return lst.empty() ? "" : *lst.begin()->first.begin();}
};

http://www.ppmy.cn/ops/115143.html

相关文章

【c++】动态内存管理

&#x1f31f;&#x1f31f;作者主页&#xff1a;ephemerals__ &#x1f31f;&#x1f31f;所属专栏&#xff1a;C 目录 前言 一、内存区域分布 二、c中的动态内存管理方式 1. new与delete对内置类型的操作 2. new与delete对自定义类型的操作 三、operator new函数和op…

基于51单片机的矿井安全检测系统

基于51单片机的矿井安全检测系统使用51单片机作为系统主控&#xff0c;LCD1602进行显示同时系统集成了ADC0808和烟雾传感器、甲烷传感器&#xff0c;二者结合测量环境烟雾值&#xff0c;同时使用DHT11温湿度传感器获取环境温湿度值&#xff0c;使用L298N驱动风扇&#xff0c;利…

Gin渲染

HTML渲染 【示例1】 首先定义一个存放模板文件的 templates文件夹&#xff0c;然后在其内部按照业务分别定义一个 posts 文件夹和一个 users 文件夹。 posts/index.tmpl {{define "posts/index.tmpl"}} <!DOCTYPE html> <html lang"en">&…

WEB攻防- Oracle基本注入

前置知识 1.dual表 此表是Oracle数据库中的一个自带表&#xff0c;为满足查询条件而产生。与MySQL不同的是&#xff0c;在MySQL中查询语句可以直接是&#xff1a;select 1,2&#xff0c;但是在Oracle中就必须跟一个表名&#xff0c;但是如查询日期是没有表的&#xff0c;就可以…

线性代数(宋浩版)(4)

2.4逆矩阵 &#xff08;不要把矩阵放在分母上&#xff09; 方阵的行列式 性质1 性质2 性质3 伴随矩阵&#xff08;只有方阵才有&#xff09; 1.求出所有元素的代数余子式&#xff08;矩阵先求行列式&#xff09;。 2.按行求的代数余子式按列放。 定理1&#xff08;重要&…

【C++指南】C++中nullptr的深入解析

&#x1f493; 博客主页&#xff1a;倔强的石头的CSDN主页 &#x1f4dd;Gitee主页&#xff1a;倔强的石头的gitee主页 ⏩ 文章专栏&#xff1a;《C指南》 期待您的关注 目录 引言 一、nullptr的引入背景 二、nullptr的特点 1.类型安全 2.明确的空指针表示 3.函数重载支…

Redis性能测试redis-benchmark

Redis性能测试redis-benchmark redis-benchmark 是 Redis 自带的性能测试工具,主要用于评估 Redis 在不同场景下的性能。以下是使用 redis-benchmark 的一些基本步骤和参数说明: 基本用法 启动测试: 在命令行中运行以下命令: redis-benchmark这将运行一系列默认的测试。 …

【设计模式-适配】

Adapter Pattern&#xff08;适配器模式&#xff09; 是一种结构型设计模式&#xff0c;其主要目的是让不兼容的接口能够协同工作。适配器模式通过引入一个适配器类&#xff0c;转换一个类的接口&#xff0c;使得原本不兼容的接口可以互相配合&#xff0c;从而实现接口的兼容性…