-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.java
73 lines (65 loc) · 2.08 KB
/
Solution.java
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
class Task {
int taskId;
String taskName;
int dueDate;
Set<String> tags;
boolean finish;
public Task(int taskId, String taskName, int dueDate, Set<String> tags) {
this.taskId = taskId;
this.taskName = taskName;
this.dueDate = dueDate;
this.tags = tags;
}
}
class TodoList {
private int i = 1;
private Map<Integer, TreeSet<Task>> tasks = new HashMap<>();
public TodoList() {
}
public int addTask(int userId, String taskDescription, int dueDate, List<String> tags) {
Task task = new Task(i++, taskDescription, dueDate, new HashSet<>(tags));
tasks.computeIfAbsent(userId, k -> new TreeSet<>(Comparator.comparingInt(a -> a.dueDate)))
.add(task);
return task.taskId;
}
public List<String> getAllTasks(int userId) {
List<String> ans = new ArrayList<>();
if (tasks.containsKey(userId)) {
for (Task task : tasks.get(userId)) {
if (!task.finish) {
ans.add(task.taskName);
}
}
}
return ans;
}
public List<String> getTasksForTag(int userId, String tag) {
List<String> ans = new ArrayList<>();
if (tasks.containsKey(userId)) {
for (Task task : tasks.get(userId)) {
if (task.tags.contains(tag) && !task.finish) {
ans.add(task.taskName);
}
}
}
return ans;
}
public void completeTask(int userId, int taskId) {
if (tasks.containsKey(userId)) {
for (Task task : tasks.get(userId)) {
if (task.taskId == taskId) {
task.finish = true;
break;
}
}
}
}
}
/**
* Your TodoList object will be instantiated and called as such:
* TodoList obj = new TodoList();
* int param_1 = obj.addTask(userId,taskDescription,dueDate,tags);
* List<String> param_2 = obj.getAllTasks(userId);
* List<String> param_3 = obj.getTasksForTag(userId,tag);
* obj.completeTask(userId,taskId);
*/