Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 64 additions & 12 deletions leetcode/3401-3500/3433.Count-Mentions-Per-User/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,80 @@
# [3433.Count Mentions Per User][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
You are given an integer `numberOfUsers` representing the total number of users and an array `events` of size `n x 3`.

Each `events[i]` can be either of the following two types:

1. **Message Event**: `["MESSAGE", "timestampi", "mentions_stringi"]`

- This event indicates that a set of users was mentioned in a message at `timestampi`.
- The `mentions_stringi` string can contain one of the following tokens:

- `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.
- `ALL`: mentions **all** users.
- `HERE`: mentions all **online** users.

2. **Offline Event**: `["OFFLINE", "timestampi", "idi"]`

- 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`.

Return an array `mentions` where `mentions[i]` represents the number of mentions the user with id `i` has across all `MESSAGE` events.

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.

**Note** that a user can be mentioned **multiple** times in a **single** message event, and each mention should be counted **separately**.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: numberOfUsers = 2, events = [["MESSAGE","10","id1 id0"],["OFFLINE","11","0"],["MESSAGE","71","HERE"]]

Output: [2,2]

Explanation:

Initially, all users are online.

At timestamp 10, id1 and id0 are mentioned. mentions = [1,1]

At timestamp 11, id0 goes offline.

At timestamp 71, id0 comes back online and "HERE" is mentioned. mentions = [2,2]
```

**Example 2:**

```
Input: numberOfUsers = 2, events = [["MESSAGE","10","id1 id0"],["OFFLINE","11","0"],["MESSAGE","12","ALL"]]

## 题意
> ...
Output: [2,2]

## 题解
Explanation:

### 思路1
> ...
Count Mentions Per User
```go
Initially, all users are online.

At timestamp 10, id1 and id0 are mentioned. mentions = [1,1]

At timestamp 11, id0 goes offline.

At timestamp 12, "ALL" is mentioned. This includes offline users, so both id0 and id1 are mentioned. mentions = [2,2]
```

**Example 3:**

```
Input: numberOfUsers = 2, events = [["OFFLINE","10","0"],["MESSAGE","12","HERE"]]

Output: [0,1]

Explanation:

Initially, all users are online.

At timestamp 10, id0 goes offline.

At timestamp 12, "HERE" is mentioned. Because id0 is still offline, they will not be mentioned. mentions = [0,1]
```

## 结语

Expand Down
81 changes: 79 additions & 2 deletions leetcode/3401-3500/3433.Count-Mentions-Per-User/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,82 @@
package Solution

func Solution(x bool) bool {
return x
import (
"sort"
"strconv"
"strings"
)

type Event struct {
Type string
Timestamp int
All, Here bool
Users []int
}

func Solution(numberOfUsers int, events [][]string) []int {
cEvents := make([]Event, 0)
var uid int
for i := range events {
t, _ := strconv.Atoi(events[i][1])
e := Event{
Type: events[i][0],
Timestamp: t,
}
if e.Type == "OFFLINE" {
uid, _ = strconv.Atoi(events[i][2])
e.Users = append(e.Users, uid)
cEvents = append(cEvents, e)
continue
}

if events[i][2] == "ALL" {
e.All = true
} else if events[i][2] == "HERE" {
e.Here = true
} else {
users := strings.Split(events[i][2], " ")
e.Users = make([]int, len(users))
for i, u := range users {
uid, _ = strconv.Atoi(u[2:])
e.Users[i] = uid
}
}

cEvents = append(cEvents, e)
}
sort.Slice(cEvents, func(i, j int) bool {
a, b := cEvents[i], cEvents[j]
if a.Timestamp == b.Timestamp {
return a.Type == "OFFLINE"
}
return a.Timestamp < b.Timestamp
})
onlineTime := make([]int, numberOfUsers)
ret := make([]int, numberOfUsers)

for _, e := range cEvents {
if e.Type == "MESSAGE" {
if e.All {
for i := range ret {
ret[i]++
}
} else if e.Here {
for i := range ret {
if e.Timestamp < onlineTime[i] {
continue
}
ret[i]++
}
} else {
for _, u := range e.Users {
ret[u]++
}
}
continue
}

onlineTime[e.Users[0]] = e.Timestamp + 60
}

return ret
}
21 changes: 11 additions & 10 deletions leetcode/3401-3500/3433.Count-Mentions-Per-User/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs int
events [][]string
expect []int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", 2, [][]string{{"MESSAGE", "10", "id1 id0"}, {"OFFLINE", "11", "0"}, {"MESSAGE", "71", "HERE"}}, []int{2, 2}},
{"TestCase2", 2, [][]string{{"MESSAGE", "10", "id1 id0"}, {"OFFLINE", "11", "0"}, {"MESSAGE", "12", "ALL"}}, []int{2, 2}},
{"TestCase3", 2, [][]string{{"OFFLINE", "10", "0"}, {"MESSAGE", "12", "HERE"}}, []int{0, 1}},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.inputs, c.events)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, got, c.inputs, c.events)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading