-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy path2590-design-a-todo-list.js
More file actions
99 lines (90 loc) · 3.09 KB
/
2590-design-a-todo-list.js
File metadata and controls
99 lines (90 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* 2590. Design a Todo List
* https://leetcode.com/problems/design-a-todo-list/
* Difficulty: Medium
*
* Design a Todo List Where users can add tasks, mark them as complete, or get a list of pending
* tasks. Users can also add tags to tasks and can filter the tasks by certain tags.
*
* Implement the TodoList class:
* - TodoList() Initializes the object.
* - int addTask(int userId, String taskDescription, int dueDate, List<String> tags) Adds a task for
* the user with the ID userId with a due date equal to dueDate and a list of tags attached to the
* task. The return value is the ID of the task. This ID starts at 1 and is sequentially
* increasing. That is, the first task's id should be 1, the second task's id should be 2, and so
* on.
* - List<String> getAllTasks(int userId) Returns a list of all the tasks not marked as complete for
* the user with ID userId, ordered by the due date. You should return an empty list if the user
* has no uncompleted tasks.
* - List<String> getTasksForTag(int userId, String tag) Returns a list of all the tasks that are
* not marked as complete for the user with the ID userId and have tag as one of their tags,
* ordered by their due date. Return an empty list if no such task exists.
* - void completeTask(int userId, int taskId) Marks the task with the ID taskId as completed only
* if the task exists and the user with the ID userId has this task, and it is uncompleted.
*/
var TodoList = function() {
this.nextTaskId = 1;
this.userTasks = new Map();
this.allTasks = new Map();
};
/**
* @param {number} userId
* @param {string} taskDescription
* @param {number} dueDate
* @param {string[]} tags
* @return {number}
*/
TodoList.prototype.addTask = function(userId, taskDescription, dueDate, tags) {
const taskId = this.nextTaskId++;
const task = {
id: taskId,
description: taskDescription,
dueDate: dueDate,
tags: new Set(tags),
completed: false
};
if (!this.userTasks.has(userId)) {
this.userTasks.set(userId, []);
}
this.userTasks.get(userId).push(task);
this.allTasks.set(taskId, { userId, task });
return taskId;
};
/**
* @param {number} userId
* @return {string[]}
*/
TodoList.prototype.getAllTasks = function(userId) {
if (!this.userTasks.has(userId)) {
return [];
}
return this.userTasks.get(userId)
.filter(task => !task.completed)
.sort((a, b) => a.dueDate - b.dueDate)
.map(task => task.description);
};
/**
* @param {number} userId
* @param {string} tag
* @return {string[]}
*/
TodoList.prototype.getTasksForTag = function(userId, tag) {
if (!this.userTasks.has(userId)) {
return [];
}
return this.userTasks.get(userId)
.filter(task => !task.completed && task.tags.has(tag))
.sort((a, b) => a.dueDate - b.dueDate)
.map(task => task.description);
};
/**
* @param {number} userId
* @param {number} taskId
* @return {void}
*/
TodoList.prototype.completeTask = function(userId, taskId) {
const taskInfo = this.allTasks.get(taskId);
if (taskInfo && taskInfo.userId === userId && !taskInfo.task.completed) {
taskInfo.task.completed = true;
}
};