From e293a88b20e608f4eecbefdd59fee51390d53215 Mon Sep 17 00:00:00 2001 From: chayan das <110921638+Chayandas07@users.noreply.github.com> Date: Sun, 29 Sep 2024 15:23:37 +0530 Subject: [PATCH] Create 432. All O`one Data Structure --- 432. All O`one Data Structure | 40 +++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 432. All O`one Data Structure diff --git a/432. All O`one Data Structure b/432. All O`one Data Structure new file mode 100644 index 0000000..be6d451 --- /dev/null +++ b/432. All O`one Data Structure @@ -0,0 +1,40 @@ +class AllOne { +public: + unordered_map count; + set> se; + AllOne() { + count.clear(); + } + + void inc(string key) { + int n = count[key]; + count[key]++; + se.erase({n,key}); + se.insert({n+1,key}); + } + void dec(string key) { + int n = count[key]; + count[key]--; + se.erase({n,key}); + if(count[key] > 0) se.insert({n-1,key}); + } + + string getMaxKey() { + if(!se.empty()) return se.rbegin()->second; + return ""; + } + + string getMinKey() { + if(!se.empty()) return se.begin()->second; + return ""; + } +}; + +/** + * 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(); + */