{"id":10244,"date":"2025-03-30T08:16:49","date_gmt":"2025-03-30T15:16:49","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=10244"},"modified":"2025-03-30T08:17:54","modified_gmt":"2025-03-30T15:17:54","slug":"leetcode-2349-design-a-number-container-system","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/desgin\/leetcode-2349-design-a-number-container-system\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2349. Design a Number Container System"},"content":{"rendered":"\n<p>\u4e24\u4e2ahashtable\uff0c\u4e00\u4e2a\u662findex->number\uff0c\u53e6\u5916\u4e00\u4e2a\u662fnumber -> {index}\uff0c\u4f7f\u7528treeset\uff0c\u81ea\u52a8\u6392\u5e8f\u3002<\/p>\n\n\n\n<p>\u65f6\u95f4\u590d\u6742\u5ea6\uff1achange O(logn)\uff0cfind O(1)<br>\u7a7a\u95f4\u590d\u6742\u5ea6\uff1aO(n)<\/p>\n\n\n\n<pre lang=\"c++\">\nclass NumberContainers {\npublic:\n  NumberContainers() {}\n  \n  void change(int index, int number) {\n    if (m_.count(index)) {\n      auto it = idx_.find(m_[index]);\n      it->second.erase(index);\n      if (it->second.empty())\n        idx_.erase(it);\n    }\n    m_[index] = number;\n    idx_[number].insert(index);\n  }\n  \n  int find(int number) {\n    auto it = idx_.find(number);\n    if (it == end(idx_)) return -1;\n    return *begin(it->second);\n  }\nprivate:\n  unordered_map<int, int> m_; \/\/ index -> num\n  unordered_map<int, set<int>> idx_; \/\/ num -> {index}\n};\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e24\u4e2ahashtable\uff0c\u4e00\u4e2a\u662findex->number\uff0c\u53e6\u5916\u4e00\u4e2a\u662fnumber -> {index}\uff0c\u4f7f\u7528treeset\uff0c\u81ea\u52a8\u6392\u5e8f\u3002 \u65f6\u95f4\u590d\u6742\u5ea6\uff1achange O(logn)\uff0cfind O(1)\u7a7a\u95f4\u590d\u6742\u5ea6\uff1aO(n) class NumberContainers { public: NumberContainers() {} void change(int index, int number) { if (m_.count(index)) { auto it = idx_.find(m_[index]);&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[340],"tags":[82,177,656],"class_list":["post-10244","post","type-post","status-publish","format-standard","hentry","category-desgin","tag-hashtable","tag-medium","tag-treeset","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10244","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=10244"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10244\/revisions"}],"predecessor-version":[{"id":10246,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10244\/revisions\/10246"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=10244"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=10244"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=10244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}