Skip to content

Commit aaf7f57

Browse files
committed
Add solution and test-cases for problem 3433
1 parent 7ce34f6 commit aaf7f57

File tree

3 files changed

+154
-24
lines changed

3 files changed

+154
-24
lines changed

leetcode/3401-3500/3433.Count-Mentions-Per-User/README.md

Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,80 @@
11
# [3433.Count Mentions Per User][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+
You are given an integer `numberOfUsers` representing the total number of users and an array `events` of size `n x 3`.
5+
6+
Each `events[i]` can be either of the following two types:
7+
8+
1. **Message Event**: `["MESSAGE", "timestampi", "mentions_stringi"]`
9+
10+
- This event indicates that a set of users was mentioned in a message at `timestampi`.
11+
- The `mentions_stringi` string can contain one of the following tokens:
12+
13+
- `id<number>`: where `<number>` is an integer in range `[0,numberOfUsers - 1]`. There can be **multiple** ids separated by a single whitespace and may contain duplicates. This can mention even the offline users.
14+
- `ALL`: mentions **all** users.
15+
- `HERE`: mentions all **online** users.
16+
17+
2. **Offline Event**: `["OFFLINE", "timestampi", "idi"]`
18+
19+
- This event indicates that the user `idi` had become offline at `timestampi` for **60 time units**. The user will automatically be online again at time `timestampi + 60`.
20+
21+
Return an array `mentions` where `mentions[i]` represents the number of mentions the user with id `i` has across all `MESSAGE` events.
22+
23+
All users are initially online, and if a user goes offline or comes back online, their status change is processed before handling any message event that occurs at the same timestamp.
24+
25+
**Note** that a user can be mentioned **multiple** times in a **single** message event, and each mention should be counted **separately**.
726

827
**Example 1:**
928

1029
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
30+
Input: numberOfUsers = 2, events = [["MESSAGE","10","id1 id0"],["OFFLINE","11","0"],["MESSAGE","71","HERE"]]
31+
32+
Output: [2,2]
33+
34+
Explanation:
35+
36+
Initially, all users are online.
37+
38+
At timestamp 10, id1 and id0 are mentioned. mentions = [1,1]
39+
40+
At timestamp 11, id0 goes offline.
41+
42+
At timestamp 71, id0 comes back online and "HERE" is mentioned. mentions = [2,2]
43+
```
44+
45+
**Example 2:**
46+
1347
```
48+
Input: numberOfUsers = 2, events = [["MESSAGE","10","id1 id0"],["OFFLINE","11","0"],["MESSAGE","12","ALL"]]
1449
15-
## 题意
16-
> ...
50+
Output: [2,2]
1751
18-
## 题解
52+
Explanation:
1953
20-
### 思路1
21-
> ...
22-
Count Mentions Per User
23-
```go
54+
Initially, all users are online.
55+
56+
At timestamp 10, id1 and id0 are mentioned. mentions = [1,1]
57+
58+
At timestamp 11, id0 goes offline.
59+
60+
At timestamp 12, "ALL" is mentioned. This includes offline users, so both id0 and id1 are mentioned. mentions = [2,2]
2461
```
2562

63+
**Example 3:**
64+
65+
```
66+
Input: numberOfUsers = 2, events = [["OFFLINE","10","0"],["MESSAGE","12","HERE"]]
67+
68+
Output: [0,1]
69+
70+
Explanation:
71+
72+
Initially, all users are online.
73+
74+
At timestamp 10, id0 goes offline.
75+
76+
At timestamp 12, "HERE" is mentioned. Because id0 is still offline, they will not be mentioned. mentions = [0,1]
77+
```
2678

2779
## 结语
2880

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,82 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import (
4+
"sort"
5+
"strconv"
6+
"strings"
7+
)
8+
9+
type Event struct {
10+
Type string
11+
Timestamp int
12+
All, Here bool
13+
Users []int
14+
}
15+
16+
func Solution(numberOfUsers int, events [][]string) []int {
17+
cEvents := make([]Event, 0)
18+
var uid int
19+
for i := range events {
20+
t, _ := strconv.Atoi(events[i][1])
21+
e := Event{
22+
Type: events[i][0],
23+
Timestamp: t,
24+
}
25+
if e.Type == "OFFLINE" {
26+
uid, _ = strconv.Atoi(events[i][2])
27+
e.Users = append(e.Users, uid)
28+
cEvents = append(cEvents, e)
29+
continue
30+
}
31+
32+
if events[i][2] == "ALL" {
33+
e.All = true
34+
} else if events[i][2] == "HERE" {
35+
e.Here = true
36+
} else {
37+
users := strings.Split(events[i][2], " ")
38+
e.Users = make([]int, len(users))
39+
for i, u := range users {
40+
uid, _ = strconv.Atoi(u[2:])
41+
e.Users[i] = uid
42+
}
43+
}
44+
45+
cEvents = append(cEvents, e)
46+
}
47+
sort.Slice(cEvents, func(i, j int) bool {
48+
a, b := cEvents[i], cEvents[j]
49+
if a.Timestamp == b.Timestamp {
50+
return a.Type == "OFFLINE"
51+
}
52+
return a.Timestamp < b.Timestamp
53+
})
54+
onlineTime := make([]int, numberOfUsers)
55+
ret := make([]int, numberOfUsers)
56+
57+
for _, e := range cEvents {
58+
if e.Type == "MESSAGE" {
59+
if e.All {
60+
for i := range ret {
61+
ret[i]++
62+
}
63+
} else if e.Here {
64+
for i := range ret {
65+
if e.Timestamp < onlineTime[i] {
66+
continue
67+
}
68+
ret[i]++
69+
}
70+
} else {
71+
for _, u := range e.Users {
72+
ret[u]++
73+
}
74+
}
75+
continue
76+
}
77+
78+
onlineTime[e.Users[0]] = e.Timestamp + 60
79+
}
80+
81+
return ret
582
}

leetcode/3401-3500/3433.Count-Mentions-Per-User/Solution_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs int
14+
events [][]string
15+
expect []int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", 2, [][]string{{"MESSAGE", "10", "id1 id0"}, {"OFFLINE", "11", "0"}, {"MESSAGE", "71", "HERE"}}, []int{2, 2}},
18+
{"TestCase2", 2, [][]string{{"MESSAGE", "10", "id1 id0"}, {"OFFLINE", "11", "0"}, {"MESSAGE", "12", "ALL"}}, []int{2, 2}},
19+
{"TestCase3", 2, [][]string{{"OFFLINE", "10", "0"}, {"MESSAGE", "12", "HERE"}}, []int{0, 1}},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.inputs, c.events)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
28+
c.expect, got, c.inputs, c.events)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)