博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] 432. All O`one Data Structure
阅读量:6857 次
发布时间:2019-06-26

本文共 3870 字,大约阅读时间需要 12 分钟。

Problem

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.

Note

Use DoublyLinkedList to get max/min key (max/min count)We put max count Bucket at tail, min count Bucket at head, O(1)So the node(Bucket) must contain count propertyAnd we can use map 
to access each node in O(1)Each Bucket could have multiple keys of same count, so use Set to store keysTo get count of a key, use Map
to achieve O(1)

Solution

class AllOne {    private class Bucket {        int count;        Set
keySet; Bucket prev; Bucket next; public Bucket(int count) { this.count = count; this.keySet = new HashSet<>(); } } private Bucket head; private Bucket tail; private Map
countBucketMap; private Map
keyCountMap; public AllOne() { head = new Bucket(Integer.MIN_VALUE); tail = new Bucket(Integer.MAX_VALUE); head.next = tail; tail.prev = head; countBucketMap = new HashMap<>(); keyCountMap = new HashMap<>(); } public void inc(String key) { if (keyCountMap.containsKey(key)) { //existing key updateKey(key, 1); } else { //new key keyCountMap.put(key, 1); if (head.next.count != 1) { addBucketAfter(head, new Bucket(1)); } head.next.keySet.add(key); countBucketMap.put(1, head.next); } } public void dec(String key) { if (keyCountMap.containsKey(key)) { int count = keyCountMap.get(key); if (count == 1) { keyCountMap.remove(key); removeKeyFromBucket(countBucketMap.get(count), key); } else { updateKey(key, -1); } } } private void updateKey(String key, int i) { int count = keyCountMap.get(key); keyCountMap.put(key, count+i); Bucket pre = countBucketMap.get(count); Bucket cur; if (countBucketMap.containsKey(count+i)) { cur = countBucketMap.get(count+i); } else { cur = new Bucket(count+i); countBucketMap.put(count+i, cur); if (i == 1) { addBucketAfter(pre, cur); } else { addBucketAfter(pre.prev, cur); } } cur.keySet.add(key); removeKeyFromBucket(pre, key); } private void addBucketAfter(Bucket pre, Bucket cur) { Bucket next = pre.next; pre.next = cur; cur.prev = pre; cur.next = next; next.prev = cur; } private void removeKeyFromBucket(Bucket bucket, String key) { bucket.keySet.remove(key); if (bucket.keySet.size() == 0) { bucket.prev.next = bucket.next; bucket.next.prev = bucket.prev; bucket.prev = null; bucket.next = null; countBucketMap.remove(bucket.count); } } public String getMaxKey() { return tail.prev == head ? "" : (String) tail.prev.keySet.iterator().next(); } public String getMinKey() { return head.next == tail ? "" : (String) head.next.keySet.iterator().next(); }}

Reference:

转载地址:http://mlnyl.baihongyu.com/

你可能感兴趣的文章
Flatten Binary Tree to Linked List
查看>>
破解TexturePacker加密资源
查看>>
IOS7开发~Images.xcassets
查看>>
硬盘助手写入文件的正确提取
查看>>
Java自定义Exception
查看>>
纯css3实现的竖形二级导航
查看>>
Oil Deposits 搜索 bfs 强联通
查看>>
SQL Server调优系列基础篇(常用运算符总结)
查看>>
人性漫画:一个人成功前和成功后赤裸裸的区别 人成功前后对比 成功人发展由来前后结果...
查看>>
压缩解压缩命令巧记
查看>>
atitit.基于组件的事件为基础的编程模型--服务器端控件(1)---------服务器端控件和标签之间的关系...
查看>>
7 Types of Regression Techniques you should know!
查看>>
自己实现一个IOC(控制翻转,DI依赖注入)容器
查看>>
Java集合概述
查看>>
android 常见分辨率(mdpi、hdpi 、xhdpi、xxhdpi )及屏幕适配注意事项
查看>>
inux下"没有设置 SVN_EDITOR...."错误解决方法
查看>>
linux普通用户权限设置为超级用户权限方法、sudo不用登陆密码
查看>>
HDU 1421 搬寝室[DP]
查看>>
二层设备与三层设备的区别--总结
查看>>
ZOJ 3829 Known Notation(字符串处理 数学 牡丹江现场赛)
查看>>