来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/all-oone-data-structure

题目描述

请你设计一个用于存储字符串计数的数据结构,并能够返回计数最小和最大的字符串。

实现 AllOne 类:

AllOne() 初始化数据结构的对象。
inc(String key) 字符串 key 的计数增加 1 。如果数据结构中尚不存在 key ,那么插入计数为 1 的 key 。
dec(String key) 字符串 key 的计数减少 1 。如果 key 的计数在减少后为 0 ,那么需要将这个 key 从数据结构中删除。测试用例保证:在减少计数前,key 存在于数据结构中。
getMaxKey() 返回任意一个计数最大的字符串。如果没有元素存在,返回一个空字符串 "" 。
getMinKey() 返回任意一个计数最小的字符串。如果没有元素存在,返回一个空字符串 "" 。

示例:

输入
["AllOne", "inc", "inc", "getMaxKey", "getMinKey", "inc", "getMaxKey", "getMinKey"]
[[], ["hello"], ["hello"], [], [], ["leet"], [], []]
输出
[null, null, null, "hello", "hello", null, "hello", "leet"]

解释
AllOne allOne = new AllOne();
allOne.inc("hello");
allOne.inc("hello");
allOne.getMaxKey(); // 返回 "hello"
allOne.getMinKey(); // 返回 "hello"
allOne.inc("leet");
allOne.getMaxKey(); // 返回 "hello"
allOne.getMinKey(); // 返回 "leet"

提示:

1 <= key.length <= 10
key 由小写英文字母组成
测试用例保证:在每次调用 dec 时,数据结构中总存在 key
最多调用 inc、dec、getMaxKey 和 getMinKey 方法 5 * 104

解题思路

题目难点在于要将操作的时间复杂度全部变为O(1),对于修改来说,很容易想到哈希表,但是哈希表的查询最大值最小值的时间复杂度并不是O(1),查询最大值和最小值的时间复杂度为O(1)的很容易想到优先队列,但是优先队列的修改时间复杂度并不是O(1),所以,这里使用一个递增的双向链表来记录所有数据,当查询最小值返回链表头,查询最大值返回链表尾,并且通过哈希表将链表结点和key值绑定,使得修改时候可以在O(1)时间内找到结点,结点内容存储的是一个set,这样可以将相同计数的结点合成一个结点,将修改的过程缩短到O(1)的时间复杂度。注意在修改时候,分类处理未出现过的key和已出现的key,还有计数为0的key。

代码展示

class AllOne {
public:
list<pair<unordered_set<string>, int>> NodeList;
unordered_map<string, list<pair<unordered_set<string>, int>>::iterator> mapstriterMap;
AllOne() { } void inc(string key) {
if(mapstriterMap.count(key))
{
auto curIter = mapstriterMap[key];
auto nextIter = next(curIter);
if(nextIter == NodeList.end() || curIter->second + 1 < nextIter->second)
{
unordered_set<string> node{key};
mapstriterMap[key] = NodeList.emplace(nextIter, node, curIter->second + 1);
}
else
{
nextIter->first.emplace(key);
mapstriterMap[key] = nextIter;
}
curIter->first.erase(key);
if(curIter->first.empty())
NodeList.erase(curIter);
}
else
{
if(NodeList.empty() || NodeList.begin()->second != 1)
{
unordered_set<string> node{key};
NodeList.emplace_front(node, 1);
}
else
{
NodeList.begin()->first.emplace(key);
}
mapstriterMap[key] = NodeList.begin();
}
} void dec(string key) {
if(mapstriterMap.count(key))
{
auto curIter = mapstriterMap[key];
auto prevIter = prev(curIter);
if(curIter == NodeList.begin())
{
if(curIter->second - 1 <= 0)
{
mapstriterMap.erase(key);
}
else
{
unordered_set<string> node{key};
NodeList.emplace_front(node, curIter->second - 1);
mapstriterMap[key] = NodeList.begin();
}
}
else if(curIter->second - 1 > prevIter->second)
{
unordered_set<string> node{key};
mapstriterMap[key] = NodeList.emplace(curIter, node, curIter->second - 1);
}
else
{
prevIter->first.emplace(key);
mapstriterMap[key] = prevIter;
}
curIter->first.erase(key);
if(curIter->first.empty())
NodeList.erase(curIter);
}
} string getMaxKey() {
return NodeList.empty()? "" : *NodeList.rbegin()->first.begin();
} string getMinKey() {
return NodeList.empty()? "" : *NodeList.begin()->first.begin();
}
}; /**
* Your AllOne object will be instantiated and called as such:
* AllOne* obj = new AllOne();
* obj->inc(key);
* obj->dec(key);
* string param_3 = obj->getMaxKey();
* string param_4 = obj->getMinKey();
*/

运行结果

LeetCode-432 全O(1)的数据结构的更多相关文章

  1. Java实现 LeetCode 432 全 O(1) 的数据结构

    432. 全 O(1) 的数据结构 实现一个数据结构支持以下操作: Inc(key) - 插入一个新的值为 1 的 key.或者使一个存在的 key 增加一,保证 key 不为空字符串. Dec(ke ...

  2. 【系统设计】432. 全 O(1) 的数据结构

    题目: 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从队列首部移除元素. peek() -- 返回队列首部的元素. empty() -- 返回队列是 ...

  3. [leetcode]432. All O`one Data Structure全O(1)数据结构

    Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...

  4. [LeetCode] All O`one Data Structure 全O(1)的数据结构

    Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...

  5. LeetCode Binary Tree Postorder Traversal(数据结构)

    题意: 用迭代法输出一棵二叉树的后序遍历结果. 思路: (1)用两个栈,一个存指针,一个存标记,表示该指针当前已经访问过哪些孩子了. /** * Definition for a binary tre ...

  6. LeetCode Implement Stack using Queues (数据结构)

    题意: 用队列来实现栈. 思路: 没有什么捷径,纯粹模拟.但是用一个队列就够了. class Stack { /* // Push element x onto stack. void push(in ...

  7. LeetCode Implement Queue using Stacks (数据结构)

    题意: 用栈来实现队列. 思路: 一个栈是不够的,至少要两个. (1)插入.永远只插入到stack1中(插到栈顶). (2)弹出.如果stack2不为空,直接弹出stack2的栈顶,否则,将stack ...

  8. C#LeetCode刷题-设计

    设计篇 # 题名 刷题 通过率 难度 146 LRU缓存机制   33.1% 困难 155 最小栈 C#LeetCode刷题之#155-最小栈(Min Stack) 44.9% 简单 173 二叉搜索 ...

  9. leetcode难题

    4 寻找两个有序数组的中位数       35.9% 困难     10 正则表达式匹配       24.6% 困难     23 合并K个排序链表       47.4% 困难     25 K ...

  10. Pandas_基础_全

    Pandas基础(全) 引言 Pandas是基于Numpy的库,但功能更加强大,Numpy专注于数值型数据的操作,而Pandas对数值型,字符串型等多种格式的表格数据都有很好的支持. 关于Numpy的 ...

随机推荐

  1. pycharm 小技巧

    ctrl键 + B 查看定义源代码 alt键 + enter键 查看帮助 ctrl键 + shift键 + -号 所有代码隐藏 ctrl键 + shift键 + +号 所有代码展示 ctrl键 + D ...

  2. nuxt之vuex的使用

    先来了解一下官网:https://www.nuxtjs.cn/guide/vuex-store 一.首先在 store 文件下新建一个index.js文件 const state = () => ...

  3. vulnhub靶场之GROTESQUE: 3.0.1

    准备: 攻击机:虚拟机kali.本机win10. 靶机:Grotesque: 3.0.1,下载地址:https://download.vulnhub.com/grotesque/grotesque3. ...

  4. 希腊字母表及latex代码

    希腊字母表及latex代码 字母大写 字母小写 英文名称 latex大写代码 latex小写代码 \(\Alpha\) \(\alpha\) alpha \Alpha \alpha \(\Beta\) ...

  5. GeoLayout: Geometry Driven Room Layout Estimation Based on Depth Maps of Planes

    1. 论文简介 论文题目:GeoLayout: Geometry Driven Room Layout Estimation Based on Depth Maps of Planes Paper地址 ...

  6. 买不到的数目【第四届蓝桥杯省赛C++A组,第四届蓝桥杯省赛JAVAC组】

    买不到的数目 小明开了一家糖果店. 他别出心裁:把水果糖包成4颗一包和7颗一包的两种. 糖果不能拆包卖. 小朋友来买糖的时候,他就用这两种包装来组合. 当然有些糖果数目是无法组合出来的,比如要买 10 ...

  7. CSS 奇思妙想之酷炫倒影

    在 CSS 中,倒影是一种比较常见的效果.今天,我们就将尝试,使用 CSS 完成各类不同的倒影效果,话不多说,直接进入主题. 实现倒影的两种方式 首先,快速过一下在 CSS 中,实现倒影的 2 种方式 ...

  8. SwiftUI(一)

    macOS 11.4 Xcode 12.5.1 1.新建工程,创建一个swiftui文件   2.创建后有些画布是在下面显示的     3.先来看下效果图   4. CardImageView.swi ...

  9. Echarts 折线图Demo调色12种,可以直接使用~~~

    测试Demo代码~~ <template> <div> <div id="myChart" ref="mapBox" style= ...

  10. 包装类总结-Collection集合概述

    包装类总结 1.基本数据类型对应的包装类byte Byteshort Shortint Integerlong Longfloat Floatdouble Doublechar Characterbo ...