Skip to content

Commit b29f3c3

Browse files
committed
Add solution and test-cases for problem 3408
1 parent f03226b commit b29f3c3

File tree

3 files changed

+163
-27
lines changed

3 files changed

+163
-27
lines changed

leetcode/3401-3500/3408.Design-Task-Manager/README.md

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,42 @@
11
# [3408.Design Task Manager][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
There is a task management system that allows users to manage their tasks, each associated with a priority. The system should efficiently handle adding, modifying, executing, and removing tasks.
75

8-
**Example 1:**
6+
Implement the `TaskManager` class:
97

10-
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
8+
- `TaskManager(vector<vector<int>>& tasks)` initializes the task manager with a list of user-task-priority triples. Each element in the input list is of the form `[userId, taskId, priority]`, which adds a task to the specified user with the given priority.
149

15-
## 题意
16-
> ...
10+
- `void add(int userId, int taskId, int priority)` adds a task with the specified `taskId` and `priority` to the user with `userId`. It is **guaranteed** that `taskId` does not exist in the system.
1711

18-
## 题解
12+
- `void edit(int taskId, int newPriority)` updates the priority of the existing `taskId` to `newPriority`. It is **guaranteed** that `taskId` exists in the system.
1913

20-
### 思路1
21-
> ...
22-
Design Task Manager
23-
```go
24-
```
14+
- `void rmv(int taskId)` removes the task identified by `taskId` from the system. It is **guaranteed** that `taskId` exists in the system.
15+
16+
- `int execTop()` executes the task with the **highest** priority across all users. If there are multiple tasks with the same **highest** priority, execute the one with the highest `taskId`. After executing, the `taskId` is **removed** from the system. Return the `userId` associated with the executed task. If no tasks are available, return -1.
2517

18+
**Note** that a user may be assigned multiple tasks.
19+
20+
**Example 1:**
21+
22+
```
23+
Input:
24+
["TaskManager", "add", "edit", "execTop", "rmv", "add", "execTop"]
25+
[[[[1, 101, 10], [2, 102, 20], [3, 103, 15]]], [4, 104, 5], [102, 8], [], [101], [5, 105, 15], []]
26+
27+
Output:
28+
[null, null, null, 3, null, null, 5]
29+
30+
Explanation
31+
32+
TaskManager taskManager = new TaskManager([[1, 101, 10], [2, 102, 20], [3, 103, 15]]); // Initializes with three tasks for Users 1, 2, and 3.
33+
taskManager.add(4, 104, 5); // Adds task 104 with priority 5 for User 4.
34+
taskManager.edit(102, 8); // Updates priority of task 102 to 8.
35+
taskManager.execTop(); // return 3. Executes task 103 for User 3.
36+
taskManager.rmv(101); // Removes task 101 from the system.
37+
taskManager.add(5, 105, 15); // Adds task 105 with priority 15 for User 5.
38+
taskManager.execTop(); // return 5. Executes task 105 for User 5.
39+
```
2640

2741
## 结语
2842

Lines changed: 115 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,119 @@
11
package Solution
22

3-
func Solution(x bool) bool {
3+
import (
4+
"container/heap"
5+
)
6+
7+
type task3408 struct {
8+
UserID, ID, Priority int
9+
index int
10+
}
11+
12+
type TaskList struct {
13+
items []*task3408
14+
}
15+
16+
func (t *TaskList) Len() int {
17+
return len(t.items)
18+
}
19+
func (t *TaskList) Swap(i, j int) {
20+
t.items[i], t.items[j] = t.items[j], t.items[i]
21+
t.items[i].index = i
22+
t.items[j].index = j
23+
}
24+
25+
func (t *TaskList) Less(i, j int) bool {
26+
a, b := t.items[i], t.items[j]
27+
if a.Priority == b.Priority {
28+
return a.ID > b.ID
29+
}
30+
return a.Priority > b.Priority
31+
}
32+
33+
func (t *TaskList) Push(x any) {
34+
item := x.(*task3408)
35+
item.index = len(t.items)
36+
t.items = append(t.items, item)
37+
}
38+
39+
func (t *TaskList) Pop() any {
40+
old := t.items
41+
l := len(old)
42+
x := old[l-1]
43+
t.items = old[:l-1]
444
return x
545
}
46+
47+
type TaskManager struct {
48+
taskHeap *TaskList
49+
taskIndex map[int]*task3408
50+
}
51+
52+
func Constructor(tasks [][]int) TaskManager {
53+
th := &TaskList{items: make([]*task3408, 0)}
54+
tm := TaskManager{taskHeap: th, taskIndex: make(map[int]*task3408)}
55+
// userId, taskId, priority
56+
for _, task := range tasks {
57+
tm.Add(task[0], task[1], task[2])
58+
}
59+
return tm
60+
}
61+
62+
func (this *TaskManager) Add(userId int, taskId int, priority int) {
63+
item := &task3408{
64+
UserID: userId,
65+
ID: taskId,
66+
Priority: priority,
67+
}
68+
this.taskIndex[taskId] = item
69+
heap.Push(this.taskHeap, item)
70+
//this.taskHeap.Push(item)
71+
}
72+
73+
func (this *TaskManager) Edit(taskId int, newPriority int) {
74+
item := this.taskIndex[taskId]
75+
item.Priority = newPriority
76+
heap.Fix(this.taskHeap, item.index)
77+
}
78+
79+
func (this *TaskManager) Rmv(taskId int) {
80+
item := this.taskIndex[taskId]
81+
delete(this.taskIndex, taskId)
82+
heap.Remove(this.taskHeap, item.index)
83+
}
84+
85+
func (this *TaskManager) ExecTop() int {
86+
if len(this.taskHeap.items) == 0 {
87+
return -1
88+
}
89+
top := heap.Pop(this.taskHeap).(*task3408)
90+
return top.UserID
91+
}
92+
93+
type op struct {
94+
name string
95+
userId, taskId, priority int
96+
}
97+
98+
func Solution(input [][]int, ops []op) []int {
99+
c := Constructor(input)
100+
var ret []int
101+
for _, o := range ops {
102+
if o.name == "add" {
103+
c.Add(o.userId, o.taskId, o.priority)
104+
continue
105+
}
106+
if o.name == "edit" {
107+
c.Edit(o.taskId, o.priority)
108+
continue
109+
}
110+
if o.name == "exec" {
111+
ret = append(ret, c.ExecTop())
112+
continue
113+
}
114+
if o.name == "rmv" {
115+
c.Rmv(o.taskId)
116+
}
117+
}
118+
return ret
119+
}

leetcode/3401-3500/3408.Design-Task-Manager/Solution_test.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,38 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs [][]int
14+
ops []op
15+
expect []int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", [][]int{
18+
{1, 101, 10}, {2, 102, 20}, {3, 103, 15},
19+
}, []op{
20+
{name: "add", userId: 4, taskId: 104, priority: 5},
21+
{name: "edit", taskId: 102, priority: 8},
22+
{name: "exec"},
23+
{name: "rmv", taskId: 101},
24+
{name: "add", userId: 5, taskId: 105, priority: 15},
25+
{name: "exec"},
26+
}, []int{3, 5}},
1927
}
2028

2129
// 开始测试
2230
for i, c := range cases {
2331
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
32+
got := Solution(c.inputs, c.ops)
2533
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
34+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
35+
c.expect, got, c.inputs, c.ops)
2836
}
2937
})
3038
}
3139
}
3240

33-
//压力测试
41+
// 压力测试
3442
func BenchmarkSolution(b *testing.B) {
3543
}
3644

37-
//使用案列
45+
// 使用案列
3846
func ExampleSolution() {
3947
}

0 commit comments

Comments
 (0)