[LeetCode] 3408. Design Task Manager

news/2025/9/19 1:45:06/文章来源:https://www.cnblogs.com/cnoodle/p/19099983

There is a task management system that allows users to manage their tasks, each associated with a priority. The system should efficiently handle adding, modifying, executing, and removing tasks.

Implement the TaskManager class:

TaskManager(vector<vector>& tasks) initializes the task manager with a list of user-task-priority triples. Each element in the input list is of the form [userId, taskId, priority], which adds a task to the specified user with the given priority.

void add(int userId, int taskId, int priority) adds a task with the specified taskId and priority to the user with userId. It is guaranteed that taskId does not exist in the system.

void edit(int taskId, int newPriority) updates the priority of the existing taskId to newPriority. It is guaranteed that taskId exists in the system.

void rmv(int taskId) removes the task identified by taskId from the system. It is guaranteed that taskId exists in the system.

int execTop() executes the task with the highest priority across all users. If there are multiple tasks with the same highest priority, execute the one with the highest taskId. After executing, the taskId is removed from the system. Return the userId associated with the executed task. If no tasks are available, return -1.

Note that a user may be assigned multiple tasks.

Example 1:
Input:
["TaskManager", "add", "edit", "execTop", "rmv", "add", "execTop"]
[[[[1, 101, 10], [2, 102, 20], [3, 103, 15]]], [4, 104, 5], [102, 8], [], [101], [5, 105, 15], []]

Output:
[null, null, null, 3, null, null, 5]

Explanation
TaskManager taskManager = new TaskManager([[1, 101, 10], [2, 102, 20], [3, 103, 15]]); // Initializes with three tasks for Users 1, 2, and 3.
taskManager.add(4, 104, 5); // Adds task 104 with priority 5 for User 4.
taskManager.edit(102, 8); // Updates priority of task 102 to 8.
taskManager.execTop(); // return 3. Executes task 103 for User 3.
taskManager.rmv(101); // Removes task 101 from the system.
taskManager.add(5, 105, 15); // Adds task 105 with priority 15 for User 5.
taskManager.execTop(); // return 5. Executes task 105 for User 5.

Constraints:
1 <= tasks.length <= 105
0 <= userId <= 105
0 <= taskId <= 105
0 <= priority <= 109
0 <= newPriority <= 109
At most 2 * 105 calls will be made in total to add, edit, rmv, and execTop methods.
The input is generated such that taskId will be valid.

设计任务管理器。

一个任务管理器系统可以让用户管理他们的任务,每个任务有一个优先级。这个系统需要高效地处理添加、修改、执行和删除任务的操作。

请你设计一个 TaskManager 类:

TaskManager(vector<vector>& tasks) 初始化任务管理器,初始化的数组格式为 [userId, taskId, priority] ,表示给 userId 添加一个优先级为 priority 的任务 taskId 。

void add(int userId, int taskId, int priority) 表示给用户 userId 添加一个优先级为 priority 的任务 taskId ,输入 保证 taskId 不在系统中。

void edit(int taskId, int newPriority) 更新已经存在的任务 taskId 的优先级为 newPriority 。输入 保证 taskId 存在于系统中。

void rmv(int taskId) 从系统中删除任务 taskId 。输入 保证 taskId 存在于系统中。

int execTop() 执行所有用户的任务中优先级 最高 的任务,如果有多个任务优先级相同且都为 最高 ,执行 taskId 最大的一个任务。执行完任务后,taskId 从系统中 删除 。同时请你返回这个任务所属的用户 userId 。如果不存在任何任务,返回 -1 。

注意 ,一个用户可能被安排多个任务。

思路

这是一道设计题,思路如下。

注意 edit 函数,因为需要对 taskId 的优先级做出修改,所以这里考虑用 hashmap 存储 <taskId, [userId, priority]> 这个信息。以taskId为 key。

因为 execTop() 函数要执行所有用户的任务中优先级 最高 的任务,所以必然牵涉到一个有序的 hashset,这里我用一个最大堆<taskId, priority>处理,堆顶元素是priority最高的任务。

复杂度

参见代码注释。

代码

Java实现

class TaskManager {// tasks, [userId, taskId, priority]// map, <taskId, [userId, priority]>HashMap<Integer, int[]> map = new HashMap<>();// pq, [taskId, priority]// 最大堆,priority大的在堆顶,否则taskId大的在堆顶PriorityQueue<int[]> queue = new PriorityQueue<>((a, b) -> {if (a[1] != b[1]) {return b[1] - a[1];}return b[0] - a[0];});public TaskManager(List<List<Integer>> tasks) {for (List<Integer> task : tasks) {add(task.get(0), task.get(1), task.get(2));}}public void add(int userId, int taskId, int priority) {map.put(taskId, new int[] { userId, priority });queue.offer(new int[] { taskId, priority });}public void edit(int taskId, int newPriority) {// [taskId, prioritys]int[] e = map.get(taskId);e[1] = newPriority; // 更新最新优先级queue.offer(new int[] { taskId, newPriority }); // 推入新副本,先不要管没有删除的那个priority}public void rmv(int taskId) {// 懒删除,不管堆里map.remove(taskId);}public int execTop() {while (!queue.isEmpty()) {int[] cur = queue.poll();int taskId = cur[0];int priority = cur[1];if (!map.containsKey(taskId)) {continue; // 已被删除}int[] e = map.get(taskId);if (e[1] != priority) {continue; // 已被更新(旧版本)}map.remove(taskId); // 执行后移除,因为taskId是唯一的return e[0]; // 返回 userId}return -1;}
}/*** Your TaskManager object will be instantiated and called as such:* TaskManager obj = new TaskManager(tasks);* obj.add(userId,taskId,priority);* obj.edit(taskId,newPriority);* obj.rmv(taskId);* int param_4 = obj.execTop();*/

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/907547.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

从0开始的游戏全栈开发工程师学习记录

从0开始的游戏全栈开发工程师学习记录开始学习游戏开发

Torrent File Editor 1.0.0

https://torrent-file-editor.github.io/ 下载:https://github.com/torrent-file-editor/torrent-file-editor/releases

US$428 XTOOL X-100 PAD Tablet Key Programmer with EEPROM Adapter Support Special Functions

XTOOL X100 PAD Tablet Key Programmer with EEPROM Adapter Support Special FunctionsNotice: 1. Language: English, Spanish, French, German, Norwegian, Russian, Persian, Arabic, Polish, Hindi and Portugues…

US$49 Multi-languages Smart Zed-Bull With Mini Type No Tokens Needed

Multi-languages Smart Zed-Bull With Mini Type No Tokens NeededTop 6 Reasons to Get the Smart Zed-Bull:1.Language:English, Turkish, Italian, Spanish and Portugues2. No Tokens Needed!3. New: Support 8C a…

US$149 Foxwell NT630 Elite ABS and Airbag Reset Tool with SAS

Foxwell NT630 Elite ABS and Airbag Reset Tool with SASUpdate Online Free Lifetime.Support Multi-Language: English, French, Spanish, Hungrian, Korean, Japanese & GermanFoxwell NT630 Elite Features a…

AI CodeReview + Devops协同

在软件开发团队里,Code Review 是非常重要的一个质量保障环境。好的 Code Review 能促进团队成长,差的 Code Review 形同流水。而在有了 LLM 之后,事情又发生了一些微妙的变化:随着代码产量上升,需要 review 的代…

【API接口】最新可用手机号归属地查询接口

最新可用手机号归属地查询接口,查询手机号码归属地、所属号段、手机卡类型、运营商等信息 使用之前您需要先去注册下key 申请地址: https://www.52api.cn 接口地址:https://www.52api.cn/api/mobile_location 返回…

【API接口】最新可用IP地址查询接口

最新可用IP地址查询接口,精准定位IPV4地址的地理位置信息,包括国家、城市、地区、运营商等详细数据,内置双线路来确保数据可用性 使用之前您需要先去注册下key 申请地址: https://www.52api.cn 接口地址:https:/…

UE5创建的对象无法用ai操控

UE5创建的对象无法用ai操控记得更改这个设置

【API接口】最新可用喜马拉雅接口

最新可用番茄畅听接口,支持搜索、详情解析、音频链接解析功能,助您快速构建您的专属听书客户端 使用之前您需要先去注册下key 申请地址: https://www.52api.cn 接口地址:https://www.52api.cn/api/xmly 返回格式:…

25/09/18 小结

第三期ccb CF519E 2100 虽然是一道2100的题,但还是比较好想的。在树上找到最短距离,明显需要用到公共祖先之类的算法,并且,还要明确的知道节点往上走几步会到哪个节点。因此,学习了dfn序求LCA的方法。 具体来说在…

【API接口】最新可用番茄畅听接口

最新可用番茄畅听接口,支持搜索、详情解析、音频链接解析功能,助您快速构建您的专属听书客户端 使用之前您需要先去注册下key 申请地址: https://www.52api.cn 接口地址:https://www.52api.cn/api/fanqie_ct 返回…

【API接口】最新可用七猫短剧接口

最新可用七猫短剧接口,支持短剧搜索、短剧详情解析、短剧播放链接解析功能,助您快速构建您的专属短剧客户端 使用之前您需要先去注册下key 申请地址: https://www.52api.cn 接口地址:https://www.52api.cn/api/qm…

磁盘分析工具推荐(Wiztree)

前言 磁盘空间占满了真难受,但是又像仓鼠一样不愿意删除,怎么找到有效的办法呢? (买买买!只要磁盘够大,都不是问题!) 但是买买买也是有上限的(除非你一直用机械硬盘去备份保存,物理存储) 所以还是需要删除文…

用FastAPI和Streamlit实现一个ChatBot

用FastAPI+Streamlit实现一个流式响应的ChatBot前言 本文使用FastAPI+Streamlit实现一个流式响应类ChatGPT的LLM应用,这里只是一个demo,后续会基于此实现一个完整的MCP Client + MCP Server的MCP应用。 Streamlit是专…

搜索百科(2):Apache Solr — 企业级搜索的开源先锋

大家好,我是 INFINI Labs 的石阳。 欢迎回到 《搜索百科》 专栏!每天 5 分钟,带你速览一款搜索相关的技术或产品,同时还会带你探索它们背后的技术原理、发展故事及上手体验等。 上一篇我们认识了搜索技术的基石 Ap…

Markbook Day03

如何打开CMDWindows+系统+终端右键+在终端打开Windows+R,输入CMD打开某个盘,在地址前输入cmd空格,然后回车 选择以管理员方式运行常用DOS命令 盘符切换 查看当前目录下所有文件dir 切换目录cd change directory cd..…

re分区为y盘,efi分区为z盘

re分区为y盘,efi分区为z盘

数组,java学习第五天

数组 数组的定义 数组是相同类型数据的有序集合 数组描述的是相同类型的若干个数据,按照一定的先后次序排列组合而成 其中,每一个数据称作一个数组元素,每个数组元素可以通过一个下标来访问它们,数组的下标是从0开…

文件结构与数据分析专项-解析

在https://exam.didctf.com/practice/questions可以找到题目出这套题主要是想鼓励大家在遇到陌生的文件时,可以主动地去对这类文件进行分析(尤其是将多个文件打包在一起),希望能通过专项练习得到这方面的提升。 源…