Practice/Meta/Leetcode 2590. Design a Todo List
Leetcode 2590. Design a Todo List
CodingOptional
Problem
Design and implement a task management system that allows multiple users to maintain their personal task lists. The system should support creating tasks with descriptions, priorities, and due dates, as well as updating, deleting, and marking tasks as complete. Additionally, it must efficiently retrieve tasks filtered by user and completion status, sorted by priority and due date.
Your TaskManager class should support the following operations:
- addTask(userId, description, priority, dueDate): Create a new task for the specified user with the given details. Priority is a positive integer (lower numbers = higher priority). Due date is represented as a positive integer. Return a unique task ID.
- updateTask(userId, taskId, description, priority, dueDate): Update an existing task's details. Return
true if the task exists and belongs to the user, false otherwise.
- deleteTask(userId, taskId): Remove a task from the system. Return
true if the task exists and belongs to the user, false otherwise.
- completeTask(userId, taskId): Mark a task as completed. Return
true if the task exists and belongs to the user, false otherwise.
- getTasks(userId, status): Retrieve all task IDs for the user with the given status ("pending" or "completed"). Results should be sorted first by priority (ascending), then by due date (ascending), and finally by task ID (ascending) for ties.
Requirements
- Task IDs must be unique across all users and auto-increment starting from 1
- Each task belongs to exactly one user
- Tasks can be in one of two states: pending or completed
- The getTasks operation must return sorted results according to the specified ordering
- Operations on non-existent tasks or tasks belonging to other users should return false
- All operations should be efficient for typical usage patterns
Constraints
- 1 ≤ userId ≤ 10⁵
- 1 ≤ description.length ≤ 100
- 1 ≤ priority ≤ 10⁹
- 1 ≤ dueDate ≤ 10⁹
- At most 10⁴ total operations will be performed
- status will be either "pending" or "completed"
- Time complexity for addTask, updateTask, deleteTask, and completeTask should be O(1) or O(log n)
- Time complexity for getTasks should be O(k log k) where k is the number of tasks returned
Examples
Example 1:
`
Input:
TaskManager()
addTask(1, "Buy milk", 2, 100)
addTask(1, "Write code", 1, 50)
getTasks(1, "pending")
Output:
1
2
[2, 1]
Explanation:
- First task gets ID 1 (priority 2, due 100)
- Second task gets ID 2 (priority 1, due 50)
- getTasks returns [2, 1] because task 2 has higher priority (1 < 2)
`
Example 2:
`
Input:
TaskManager()
addTask(1, "Task A", 1, 5)
addTask(1, "Task B", 1, 3)
completeTask(1, 1)
getTasks(1, "pending")
getTasks(1, "completed")
Output:
1
2
true
[2]
[1]
Explanation:
- Task 1 is marked complete
- Pending tasks only returns task 2
- Completed tasks only returns task 1
`
Example 3:
`
Input:
TaskManager()
addTask(1, "User 1 Task", 2, 5)
addTask(2, "User 2 Task", 1, 3)
deleteTask(1, 2)
getTasks(1, "pending")
Output:
1
2
false
[1]
Explanation:
- deleteTask returns false because task 2 belongs to user 2, not user 1
- User 1 still has task 1 pending
`