• LeetCode-432. All O`one Data Structure [C++][Java]


    LeetCode-432. All O`one Data StructureLevel up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.https://leetcode.com/problems/all-oone-data-structure/

    Design a data structure to store the strings' count with the ability to return the strings with minimum and maximum counts.

    Implement the AllOne class:

    • AllOne() Initializes the object of the data structure.
    • inc(String key) Increments the count of the string key by 1. If key does not exist in the data structure, insert it with count 1.
    • dec(String key) Decrements the count of the string key by 1. If the count of key is 0 after the decrement, remove it from the data structure. It is guaranteed that key exists in the data structure before the decrement.
    • getMaxKey() Returns one of the keys with the maximal count. If no element exists, return an empty string "".
    • getMinKey() Returns one of the keys with the minimum count. If no element exists, return an empty string "".

    Note that each function must run in O(1) average time complexity.

    Example 1:

    Input
    ["AllOne", "inc", "inc", "getMaxKey", "getMinKey", "inc", "getMaxKey", "getMinKey"]
    [[], ["hello"], ["hello"], [], [], ["leet"], [], []]
    Output
    [null, null, null, "hello", "hello", null, "hello", "leet"]
    
    Explanation
    AllOne allOne = new AllOne();
    allOne.inc("hello");
    allOne.inc("hello");
    allOne.getMaxKey(); // return "hello"
    allOne.getMinKey(); // return "hello"
    allOne.inc("leet");
    allOne.getMaxKey(); // return "hello"
    allOne.getMinKey(); // return "leet"
    

    Constraints:

    • 1 <= key.length <= 10
    • key consists of lowercase English letters.
    • It is guaranteed that for each call to deckey is existing in the data structure.
    • At most 5 * 10^4 calls will be made to incdecgetMaxKey, and getMinKey.

    【C++】

    1. class AllOne {
    2. unordered_mapint> m;// store string -> val
    3. map<int,unordered_set> mp;// val -> set of strings with value as val
    4. public:
    5. AllOne() {}
    6. void inc(string key) {
    7. if (m.find(key) == m.end()) {
    8. m[key]=1;
    9. mp[1].insert(key);
    10. } else {
    11. int p=m[key];
    12. m[key]=p+1;
    13. mp[p].erase(key);
    14. if (mp[p].size() == 0) {mp.erase(p);}
    15. mp[p+1].insert(key);
    16. }
    17. }
    18. void dec(string key) {
    19. int p=m[key];
    20. if (p==1) {
    21. m.erase(key);
    22. mp[p].erase(key);
    23. if(mp[p].size()==0) {mp.erase(p);}
    24. } else {
    25. m[key]=p-1;
    26. mp[p].erase(key);
    27. if (mp[p].size() == 0) {mp.erase(p);}
    28. mp[p-1].insert(key);
    29. }
    30. }
    31. string getMaxKey() {
    32. string s="";
    33. if(mp.size()) {
    34. auto p=mp.rbegin()->second.begin();
    35. s=*p;
    36. }
    37. return s;
    38. }
    39. string getMinKey() {
    40. string s="";
    41. if (mp.size()) {
    42. auto p=mp.begin()->second.begin();
    43. s=*p;
    44. }
    45. return s;
    46. }
    47. };
    48. /**
    49. * Your AllOne object will be instantiated and called as such:
    50. * AllOne* obj = new AllOne();
    51. * obj->inc(key);
    52. * obj->dec(key);
    53. * string param_3 = obj->getMaxKey();
    54. * string param_4 = obj->getMinKey();
    55. */

    【Java】

    1. class AllOne {
    2. static class Bucket {
    3. int count;
    4. Set keys;
    5. Bucket prev;
    6. Bucket next;
    7. public Bucket(int count) {
    8. this.count = count;
    9. keys = new LinkedHashSet<>();
    10. }
    11. public void insertAfter(Bucket newBucket) {
    12. Bucket nextBucket = this.next;
    13. this.next = newBucket;
    14. nextBucket.prev = newBucket;
    15. newBucket.prev = this;
    16. newBucket.next = nextBucket;
    17. }
    18. }
    19. private final Map keysToBucket;
    20. private final Bucket head;
    21. private final Bucket tail;
    22. public AllOne() {
    23. keysToBucket = new HashMap<>();
    24. head = new Bucket(0);
    25. tail = new Bucket(0);
    26. head.next = tail;
    27. tail.prev = head;
    28. }
    29. public void inc(String key) {
    30. Bucket currBucket = keysToBucket.getOrDefault(key, head);
    31. Bucket nextBucket = currBucket.next;
    32. if (nextBucket.count != currBucket.count + 1) {
    33. nextBucket = new Bucket(currBucket.count + 1);
    34. currBucket.insertAfter(nextBucket);
    35. }
    36. nextBucket.keys.add(key);
    37. removeKeyFromBucket(currBucket, key);
    38. keysToBucket.put(key, nextBucket);
    39. }
    40. public void dec(String key) {
    41. Bucket currBucket = keysToBucket.get(key), prevBucket = currBucket.prev;
    42. if (prevBucket.count != currBucket.count - 1) {
    43. prevBucket = new Bucket(currBucket.count - 1);
    44. currBucket.prev.insertAfter(prevBucket);
    45. }
    46. if (currBucket.count != 1) {prevBucket.keys.add(key);}
    47. removeKeyFromBucket(currBucket, key);
    48. keysToBucket.put(key, prevBucket);
    49. }
    50. public String getMaxKey() {
    51. if (tail.prev == head) {return "";}
    52. return tail.prev.keys.iterator().next();
    53. }
    54. public String getMinKey() {
    55. if (head.next == tail) {return "";}
    56. return head.next.keys.iterator().next();
    57. }
    58. private void deleteBucket(Bucket bucket) {
    59. Bucket prev = bucket.prev, next = bucket.next;
    60. prev.next = next;
    61. next.prev = prev;
    62. bucket.next = null;
    63. bucket.prev = null;
    64. }
    65. private void removeKeyFromBucket(Bucket bucket, String key) {
    66. if (bucket.count > 0) {
    67. bucket.keys.remove(key);
    68. if (bucket.keys.isEmpty()) {deleteBucket(bucket);}
    69. }
    70. }
    71. }
    72. /**
    73. * Your AllOne object will be instantiated and called as such:
    74. * AllOne obj = new AllOne();
    75. * obj.inc(key);
    76. * obj.dec(key);
    77. * String param_3 = obj.getMaxKey();
    78. * String param_4 = obj.getMinKey();
    79. */

  • 相关阅读:
    Steam下载MOD至本地文件夹
    【VMware vSAN】创建vSAN Max集群并配置挂载远程数据存储。
    网关Gateway的介绍与使用
    eyb:职称管理页面设计到部门删除功能实现(三)
    RuntimeError: “addmm_impl_cpu_“ not implemented for ‘Half‘
    k8s的接口文档——swagger-ui服务
    GaN HEMT 电容的分析建模,包括寄生元件
    评价指标篇——IOU(交并比)
    数据可视化训练第四天(模拟投掷筛子并且统计频次)
    20220809模拟
  • 原文地址:https://blog.csdn.net/qq_15711195/article/details/126458620