zoukankan      html  css  js  c++  java
  • LeetCode 432. All O`one Data Structure

    原题链接在这里:https://leetcode.com/problems/all-oone-data-structure/?tab=Description

    题目:

    Implement a data structure supporting the following operations:

    1. 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.
    2. 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.
    3. GetMaxKey() - Returns one of the keys with maximal value. If no element exists, return an empty string "".
    4. 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.

    题解:

    很像LFU的机制.

    用Bucket组成一个double linked list. 按照Bucket的count从小到大排序.

    inc 把key move到下一个Bucket.

    dec把key move到前一个Bucket.

    min key就在前面, head.next.

    max key就出现在后面, tail.pre.

    Time Complexity: inc, dec, getaMaxKey, getMinKey, O(1).

    Space: O(n). n是现有key的个数.

    AC Java:

      1 public class AllOne {
      2     Bucket head;
      3     Bucket tail;
      4     Map<String, Integer> keyCount;
      5     Map<Integer, Bucket> countBucket;
      6     
      7     /** Initialize your data structure here. */
      8     public AllOne() {
      9         head = new Bucket(-1);
     10         tail = new Bucket(-1);
     11         head.next = tail;
     12         tail.pre = head;
     13         keyCount = new HashMap<String, Integer>();
     14         countBucket = new HashMap<Integer, Bucket>();
     15     }
     16     
     17     /** Inserts a new key <Key> with value 1. Or increments an existing key by 1. */
     18     public void inc(String key) {
     19         if(keyCount.containsKey(key)){
     20             moveKey(key, 1);
     21         }else{
     22             keyCount.put(key, 1);
     23             if(head.next.count != 1){
     24                 addBucketAfter(new Bucket(1), head);
     25             }
     26             head.next.keySet.add(key);
     27             countBucket.put(1, head.next);
     28         }
     29     }
     30     
     31     /** Decrements an existing key by 1. If Key's value is 1, remove it from the data structure. */
     32     public void dec(String key) {
     33         if(keyCount.containsKey(key)){
     34             int count = keyCount.get(key);
     35             if(count == 1){
     36                 keyCount.remove(key);
     37                 removeKeyFromBucket(countBucket.get(count), key);
     38             }else{
     39                 moveKey(key, -1);
     40             }
     41         }
     42     }
     43     
     44     /** Returns one of the keys with maximal value. */
     45     public String getMaxKey() {
     46         return tail.pre == head ? "" : tail.pre.keySet.iterator().next();
     47     }
     48     
     49     /** Returns one of the keys with Minimal value. */
     50     public String getMinKey() {
     51         return head.next == tail ? "" : head.next.keySet.iterator().next();
     52     }
     53     
     54     private void moveKey(String key, int offset){
     55         int count = keyCount.get(key);
     56         keyCount.put(key, count+offset);
     57         Bucket cur = countBucket.get(count);
     58         Bucket moveToBucket;
     59         if(countBucket.containsKey(count+offset)){
     60             moveToBucket = countBucket.get(count+offset);
     61         }else{
     62             moveToBucket = new Bucket(count+offset);
     63             countBucket.put(count+offset, moveToBucket);
     64             addBucketAfter(moveToBucket, offset == 1 ? cur : cur.pre);
     65         }
     66         
     67         moveToBucket.keySet.add(key);
     68         removeKeyFromBucket(cur, key);
     69     }
     70     
     71     private void addBucketAfter(Bucket newBucket, Bucket preBucket){
     72         newBucket.next = preBucket.next;
     73         newBucket.pre = preBucket;
     74         preBucket.next.pre = newBucket;
     75         preBucket.next = newBucket;
     76     }
     77     
     78     private void removeKeyFromBucket(Bucket cur, String key){
     79         cur.keySet.remove(key);
     80         if(cur.keySet.size() == 0){
     81             countBucket.remove(cur.count);
     82             cur.pre.next = cur.next;
     83             cur.next.pre = cur.pre;
     84         }
     85     }
     86 }
     87 
     88 class Bucket{
     89     int count;
     90     Set<String> keySet;
     91     Bucket pre;
     92     Bucket next;
     93     
     94     public Bucket(int count){
     95         this.count = count;
     96         keySet = new LinkedHashSet<String>();
     97     }
     98 }
     99 
    100 /**
    101  * Your AllOne object will be instantiated and called as such:
    102  * AllOne obj = new AllOne();
    103  * obj.inc(key);
    104  * obj.dec(key);
    105  * String param_3 = obj.getMaxKey();
    106  * String param_4 = obj.getMinKey();
    107  */

    类似LFU Cache.

  • 相关阅读:
    DeFi之道丨科普:一分钟了解以太坊layer2扩容
    4.B-Air302(NB-IOT)-功能扩展-Android扫码绑定Air302,并通过MQTT实现远程控制和监控PLC(三菱Fx1s-10MR)
    Air302(NB-IOT)-硬件BUG-电路硬件错误及其修改说明
    4.2-Air302(NB-IOT)-自建MQTT服务器-Android扫码绑定Air302,并通过MQTT实现远程通信控制
    Android 开发:数据库操作-android 使用 litepal 操作本地数据库
    0.5-Air302(NB-IOT)-连接自建MQTT服务器
    0.1-Air302(NB-IOT)-刷AT指令固件
    STM32+ESP8266+AIR202基本控制篇-00-开发板回收说明
    21-STM32+ESP8266+AIR202/302远程升级方案-扩展应用-移植远程升级包实现STM32F407VET6+串口网络模组(ESP8266/Air202/Air302)使用http或者https远程升级单片机程序
    数据处理思想和程序架构: 单片机STM32F407xx/F405xx/F415xx/417xx系列flash存储方案
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/6491294.html
Copyright © 2011-2022 走看看