432. All O`one Data Structure
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.
思路: 这道题关键在于如何保存数据形式: 1、使用一个Hashmap保存当前存入的key的频次frequency 2、倒序使用一个Treemap保存当前frequency为某个值时的所有key有哪些,Treemap按照key排序。 因此在inc和dec时只需要正常的更新两个map就可以,在取最大最小的时候,直接用有序的treemap的第一个key和最后一个key就可以(分别对应最小的frequency和最大的frequency),然后再将数据取出来就好。
class AllOne { TreeMap> reversedIndex; HashMap index; /** Initialize your data structure here. */ public AllOne() { this.reversedIndex = new TreeMap<>(); this.index = new HashMap<>(); } /** Inserts a new 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()); 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(); }}/** * Your AllOne object will be instantiated and called as such: * AllOne obj = new AllOne(); * obj.inc(key); * obj.dec(key); * String param_3 = obj.getMaxKey(); * String param_4 = obj.getMinKey(); */
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
暂时没有评论,来抢沙发吧~