Skip to content

Commit a5137e4

Browse files
authored
Merge pull request #1322 from 0xff-dev/1817
Add solution and test-cases for problem 1817
2 parents fe153ab + f97c8f7 commit a5137e4

File tree

3 files changed

+49
-26
lines changed

3 files changed

+49
-26
lines changed

leetcode/1801-1900/1817.Finding-the-Users-Active-Minutes/README.md

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,38 @@
11
# [1817.Finding the Users Active Minutes][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 the logs for users' actions on LeetCode, and an integer `k`. The logs are represented by a 2D integer array `logs` where each `logs[i] = [IDi, timei]` indicates that the user with IDi performed an action at the minute `timei`.
5+
6+
**Multiple users** can perform actions simultaneously, and a single user can perform **multiple actions** in the same minute.
7+
8+
The **user active minutes (UAM)** for a given user is defined as the **number of unique minutes** in which the user performed an action on LeetCode. A minute can only be counted once, even if multiple actions occur during it.
9+
10+
You are to calculate a **1-indexed** array `answer` of size `k` such that, for each `j` (`1 <= j <= k`), `answer[j]` is the **number of users** whose **UAM** equals `j`.
11+
12+
Return the array `answer` as described above.
713

814
**Example 1:**
915

1016
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
17+
Input: logs = [[0,5],[1,2],[0,2],[0,5],[1,3]], k = 5
18+
Output: [0,2,0,0,0]
19+
Explanation:
20+
The user with ID=0 performed actions at minutes 5, 2, and 5 again. Hence, they have a UAM of 2 (minute 5 is only counted once).
21+
The user with ID=1 performed actions at minutes 2 and 3. Hence, they have a UAM of 2.
22+
Since both users have a UAM of 2, answer[2] is 2, and the remaining answer[j] values are 0.
1323
```
1424

15-
## 题意
16-
> ...
17-
18-
## 题解
25+
**Example 2:**
1926

20-
### 思路1
21-
> ...
22-
Finding the Users Active Minutes
23-
```go
2427
```
25-
28+
Input: logs = [[1,1],[2,2],[2,3]], k = 4
29+
Output: [1,1,0,0]
30+
Explanation:
31+
The user with ID=1 performed a single action at minute 1. Hence, they have a UAM of 1.
32+
The user with ID=2 performed actions at minutes 2 and 3. Hence, they have a UAM of 2.
33+
There is one user with a UAM of 1 and one with a UAM of 2.
34+
Hence, answer[1] = 1, answer[2] = 1, and the remaining values are 0.
35+
```
2636

2737
## 结语
2838

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(logs [][]int, k int) []int {
4+
ret := make([]int, k)
5+
// 1:1
6+
// 2:2
7+
count := make(map[int]map[int]struct{})
8+
for _, log := range logs {
9+
if _, ok := count[log[0]]; !ok {
10+
count[log[0]] = make(map[int]struct{})
11+
}
12+
count[log[0]][log[1]] = struct{}{}
13+
}
14+
for _, c := range count {
15+
ret[len(c)-1]++
16+
}
17+
return ret
518
}

leetcode/1801-1900/1817.Finding-the-Users-Active-Minutes/Solution_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
logs [][]int
14+
k int
15+
expect []int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", [][]int{{0, 5}, {1, 2}, {0, 2}, {0, 5}, {1, 3}}, 5, []int{0, 2, 0, 0, 0}},
18+
{"TestCase2", [][]int{{1, 1}, {2, 2}, {2, 3}}, 4, []int{1, 1, 0, 0}},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.logs, c.k)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.logs, c.k)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)