成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[LeetCode] 432. All O`one Data Structure

tanglijun / 2879人閱讀

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 property
And 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 keys

To 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: https://leetcode.com/problems...

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/72309.html

相關(guān)文章

  • [LeetCode]Find Median from Data Stream

    Find Median from Data Stream Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value. Examp...

    suemi 評(píng)論0 收藏0
  • leetcode-211-Add and Search Word - Data structure

    摘要:原題總結(jié)棧的利用,先進(jìn)后出的作用,可以保持鏈表一類的數(shù)據(jù)的連貫操作,可以用來(lái)替代廣度搜索。每一層次可以用進(jìn)棧出棧進(jìn)行替代。形式的數(shù)據(jù)結(jié)構(gòu),有記憶狀態(tài)的作用。應(yīng)用字符串的遍歷,廣度搜索。 原題: 211. Add and Search Word - Data structure design Design a data structure that supports the follo...

    Alliot 評(píng)論0 收藏0
  • [LeetCode] 348. Design Tic-Tac-Toe

    Problem Design a Tic-tac-toe game that is played between two players on a n x n grid. You may assume the following rules: A move is guaranteed to be valid and is placed on an empty block.Once a winnin...

    MobService 評(píng)論0 收藏0
  • [Leetcode] Find Median from Data Stream 數(shù)據(jù)流中位數(shù)

    摘要:最大堆存的是到目前為止較小的那一半數(shù),最小堆存的是到目前為止較大的那一半數(shù),這樣中位數(shù)只有可能是堆頂或者堆頂兩個(gè)數(shù)的均值。我們將新數(shù)加入堆后,要保證兩個(gè)堆的大小之差不超過(guò)。最大堆堆頂大于新數(shù)時(shí),說(shuō)明新數(shù)將處在所有數(shù)的下半部分。 Data Stream Median 最新更新:https://yanjia.me/zh/2019/02/... Median is the middle v...

    heartFollower 評(píng)論0 收藏0
  • [LintCode/LeetCode] Min Stack/Max Stack

    Problem Implement a stack with min() function, which will return the smallest number in the stack. It should support push, pop and min operation all in O(1) cost. Example push(1)pop() // return 1pus...

    GHOST_349178 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<