Leetcode 432. All O`one Data Structure 高效的数据结构 解题报告

1 解题思想

题目要求建立一个数据结构,使得对数据的增减,取当前最大最小的时间复杂度都是o(1)
题目关键难度在于时间复杂度的要求(在取最大最小时的设计)

这道题关键在于如何保存数据形式:
1、使用一个Hashmap保存当前存入的key的频次frequency
2、倒序使用一个Treemap保存当前frequency为某个值时的所有key有哪些,Treemap按照key排序

因此在inc和dec时只需要正常的更新两个map就可以,在取最大最小的时候,直接用有序的treemap的第一个key和最后一个key就可以(分别对应最小的frequency和最大的frequency),然后再将数据取出来就好。

当然其实Treemap的排序开销才开始并不是O(1),但如果数据量大,其实也近似O(1)了。。数据量小又不影响~~~

2 原题

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.

3 AC解

class AllOne {
    /**
     * 核心在于构建两个索引,一个是 key-fre,一个是fre-set(key) 这样才能实现快速的查找
     */
    TreeMap<Integer,HashSet<String>> reversedIndex;
    HashMap<String,Integer> index;
    /** Initialize your data structure here. */
    public AllOne() {
        this.reversedIndex = new TreeMap<>();
        this.index = new HashMap<>();
    }

    /** Inserts a new key <Key> with value 1. Or increments an existing key by 1. */

    public void inc(String key) {
        if (this.index.containsKey(key) == false){
            this.index.put(key,1);
            if(this.reversedIndex.containsKey(1) == false)
                this.reversedIndex.put(1,new HashSet<String>());
            this.reversedIndex.get(1).add(key);
        } else{
            int currentCounts = this.index.get(key);
            this.reversedIndex.get(currentCounts).remove(key);
            // 这里必须要做remove,因为treemap要直接firstkey()或者lastkey,下面dec同理
            if(this.reversedIndex.get(currentCounts).size() == 0){
                this.reversedIndex.remove(currentCounts);
            }
            if(this.reversedIndex.containsKey(currentCounts + 1) == false){
                this.reversedIndex.put(currentCounts + 1,new HashSet<>());
            }
            this.index.put(key,currentCounts + 1);
            this.reversedIndex.get(currentCounts + 1).add(key);
        }
    }

    /** Decrements an existing key by 1. If Key's value is 1, remove it from the data structure. */
    public void dec(String key) {
        if(this.index.containsKey(key)){
            int currentCounts = this.index.get(key);
            this.reversedIndex.get(currentCounts).remove(key);
            if(this.reversedIndex.get(currentCounts).size() == 0){
                this.reversedIndex.remove(currentCounts);
            }
            if(currentCounts == 1){
                this.index.remove(key);
            } else{
                if(this.reversedIndex.containsKey(currentCounts - 1) == false){
                    this.reversedIndex.put(currentCounts - 1,new HashSet<>());
                }
                this.reversedIndex.get(currentCounts -1).add(key);
                this.index.put(key,currentCounts - 1);
            }
        }
    }

    /** Returns one of the keys with maximal value. */
    public String getMaxKey() {
        if (this.index.size() == 0 ) return "";
        return this.reversedIndex.get(this.reversedIndex.lastKey()).iterator().next();
    }

    /** Returns one of the keys with Minimal value. */
    public String getMinKey() {
        if (this.index.size() == 0 ) return "";
        return this.reversedIndex.get(this.reversedIndex.firstKey()).iterator().next();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值