-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
114 lines (103 loc) · 3.49 KB
/
Solution.cs
File metadata and controls
114 lines (103 loc) · 3.49 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
public class TaskManager
{
PriorityQueue<(int userId, int taskId, int priority), (int taskId, int priority)> pq = new(Comparer<(int taskId, int priority)>.Create((a, b) =>
{
if (a.priority == b.priority) return b.taskId.CompareTo(a.taskId);
return b.priority.CompareTo(a.priority);
}));
Dictionary<int, int> mapTaskIdToUser = [];
Dictionary<int, int> mapTaskIdToPriority = [];
public TaskManager(IList<IList<int>> tasks)
{
foreach (IList<int> task in tasks)
{
int userId = task[0];
int taskId = task[1];
int priority = task[2];
Add(userId, taskId, priority);
}
}
public void Add(int userId, int taskId, int priority)
{
pq.Enqueue((userId, taskId, priority), (taskId, priority));
mapTaskIdToUser[taskId] = userId;
mapTaskIdToPriority[taskId] = priority;
}
public void Edit(int taskId, int newPriority)
{
mapTaskIdToPriority[taskId] = newPriority;
int userId = mapTaskIdToUser[taskId];
pq.Enqueue((userId, taskId, newPriority), (taskId, newPriority));
}
public void Rmv(int taskId)
{
mapTaskIdToPriority.Remove(taskId);
mapTaskIdToUser.Remove(taskId);
}
public int ExecTop()
{
while (pq.Count > 0)
{
var (userId, taskId, priority) = pq.Dequeue();
if (mapTaskIdToPriority.TryGetValue(taskId, out int curPriority) && mapTaskIdToUser.TryGetValue(taskId, out int curUserId))
{
if (curPriority == priority && curUserId == userId)
{
Rmv(taskId);
return 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();
*/
public class Solution
{
public List<dynamic> Execute(string[] actions, dynamic values)
{
List<dynamic> result = [];
object[] objectList = JsonConvert.DeserializeObject<object[]>(values);
TaskManager taskManager = null;
for (int i = 0; i < actions.Length; i++)
{
switch (actions[i])
{
case "TaskManager":
int[][][] inputs = CastType<int[][]>(objectList[i]);
taskManager = new TaskManager(inputs[0]);
result.Add(null);
break;
case "add":
taskManager.Add(CastType<int>(objectList[i])[0], CastType<int>(objectList[i])[1], CastType<int>(objectList[i])[2]);
result.Add(null);
break;
case "rmv":
taskManager.Rmv(CastType<int>(objectList[i])[0]);
result.Add(null);
break;
case "execTop":
result.Add(taskManager.ExecTop());
break;
case "edit":
taskManager.Edit(CastType<int>(objectList[i])[0], CastType<int>(objectList[i])[1]);
result.Add(null);
break;
default:
break;
}
}
return result;
}
private T[] CastType<T>(object value)
{
return JsonConvert.DeserializeObject<T[]>(JsonConvert.SerializeObject(value));
}
}