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.

  • 相关阅读:
    转dhdhtmlxTree
    转Merge的用法
    解决SqlServer2008评估期过期
    借鉴一下对比算法
    Asp.Net异常:"由于代码已经过优化或者本机框架位于调用堆栈之上,无法计算表达式的值"的解决方法
    查看Windows下引起Oracle CPU占用率高的神器-qslice
    今日有奖活动一览
    【分享】给做技术的战友们推荐一个不错的微信公号解解闷
    Unreal Engine Plugin management
    当在ECLIPSE中import现存项目时,如遇到版本不符
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/6491294.html
Copyright © 2011-2022 走看看