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.

  • 相关阅读:
    c++内存管理5-虚拟内存4区结构图
    C++内存管理5-处理new分配内存失败情况(转)
    C++内存管理4-Windows编程中的堆管理(转)
    C++内存管理3-探讨C++内存和回收
    C++内存管理2-内存泄漏
    VS2015远程调试
    C++内存管理1-64位系统运行32位软件会占用更多的内存吗?
    ffmpeg安装步骤
    golang字符串拼接
    如何严格设置php中session过期时间
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/6491294.html
Copyright © 2011-2022 走看看