LeetCode 2590: Design a Todo List Solution
Master LeetCode problem 2590 (Design a Todo List), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
2590. Design a Todo List
Problem Explanation
Explanation:
To design a Todo List system, we can use a Priority Queue to store the tasks based on their priority. Each task can have a priority level, a description, and a deadline. The Priority Queue will ensure that the tasks are ordered based on their priority level, with higher priority tasks being processed first. We can implement methods to add a new task, mark a task as completed, and get the next task to work on.
-
Add Task (with priority, description, and deadline):
- Create a Task object with the given priority, description, and deadline.
- Add the Task to the Priority Queue based on its priority level.
-
Mark Task as Completed:
- Remove the highest priority task from the Priority Queue (indicating it has been completed).
-
Get Next Task:
- Peek at the highest priority task in the Priority Queue to get the next task to work on.
Time Complexity Analysis:
- Adding a task: O(log n) due to adding to the Priority Queue.
- Marking a task as completed: O(log n) due to removing from the Priority Queue.
- Getting the next task: O(1) to peek at the highest priority task.
Space Complexity:
- O(n) where n is the number of tasks in the Priority Queue.
:
Solution Code
import java.util.PriorityQueue;
class Task {
int priority;
String description;
String deadline;
public Task(int priority, String description, String deadline) {
this.priority = priority;
this.description = description;
this.deadline = deadline;
}
}
class TodoList {
PriorityQueue<Task> tasks;
public TodoList() {
tasks = new PriorityQueue<>((a, b) -> b.priority - a.priority);
}
public void addTask(int priority, String description, String deadline) {
tasks.offer(new Task(priority, description, deadline));
}
public void markTaskAsCompleted() {
tasks.poll();
}
public Task getNextTask() {
return tasks.peek();
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 2590 (Design a Todo List)?
This page provides optimized solutions for LeetCode problem 2590 (Design a Todo List) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 2590 (Design a Todo List)?
The time complexity for LeetCode 2590 (Design a Todo List) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 2590 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2590 in Java, C++, or Python.