{"id":10229,"date":"2025-03-27T20:45:03","date_gmt":"2025-03-28T03:45:03","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=10229"},"modified":"2025-03-27T20:46:44","modified_gmt":"2025-03-28T03:46:44","slug":"leetcode-3408-design-task-manager","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/priority-queue\/leetcode-3408-design-task-manager\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 3408. Design Task Manager"},"content":{"rendered":"\n<ol class=\"wp-block-list\"><li>pq_ \u4f7f\u7528std::map\u5145\u5f53\u4f18\u5148\u961f\u5217\uff0c\u5b58\u50a8(priority, taskId) -> userId<\/li><li>m_ \u4f7f\u7528std::unordered_map\uff0c\u5b58\u50a8taskId -> pq_\u7684\u8fed\u4ee3\u5668<\/li><\/ol>\n\n\n\n<p>\u6240\u6709\u64cd\u4f5c\u90fd\u662fO(logn)<\/p>\n\n\n\n<pre lang=\"c++\">\nclass TaskManager {\npublic:\n    TaskManager(vector<vector<int>>& tasks) {\n      for (const auto& task : tasks)\n        add(task[0], task[1], task[2]);\n    }\n\n    void add(int userId, int taskId, int priority) {\n      m_[taskId] = pq_.emplace(make_pair(priority, taskId), userId).first;\n    }\n    \n    void edit(int taskId, int newPriority) {\n      const int userId = m_[taskId]->second;\n      rmv(taskId);\n      add(userId, taskId, newPriority);\n    }\n    \n    void rmv(int taskId) {\n      auto it = m_.find(taskId);\n      pq_.erase(it->second);\n      m_.erase(it);\n    }\n    \n    int execTop() {\n      if (pq_.empty()) return -1;\n      auto it = pq_.rbegin();\n      const int userId = it->second;\n      const int taskId = (it->first.second);\n      rmv(taskId);\n      return userId;\n    }\n  private:\n    map<std::pair<int,int>, int> pq_; \/\/ (priority, taskId) -> userId;\n    unordered_map<int, map<std::pair<int,int>, int>::iterator> m_; \/\/ taskId -> pq iterator\n};\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>pq_ \u4f7f\u7528std::map\u5145\u5f53\u4f18\u5148\u961f\u5217\uff0c\u5b58\u50a8(priority, taskId) -> userId m_ \u4f7f\u7528std::unordered_map\uff0c\u5b58\u50a8taskId -> pq_\u7684\u8fed\u4ee3\u5668 \u6240\u6709\u64cd\u4f5c\u90fd\u662fO(logn) class TaskManager { public: TaskManager(vector&#038; tasks) { for (const auto&#038; task : tasks) add(task[0], task[1],&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[674],"tags":[82,625,462,177,72,699],"class_list":["post-10229","post","type-post","status-publish","format-standard","hentry","category-priority-queue","tag-hashtable","tag-iterator","tag-map","tag-medium","tag-priority-queue","tag-treemap","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10229","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=10229"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10229\/revisions"}],"predecessor-version":[{"id":10231,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10229\/revisions\/10231"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=10229"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=10229"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=10229"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}