From f97c8f7e664cdddecaac4eee5aa4d1684b223e81 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Tue, 23 Sep 2025 09:16:03 +0800 Subject: [PATCH] Add solution and test-cases for problem 1817 --- .../README.md | 38 ++++++++++++------- .../Solution.go | 17 ++++++++- .../Solution_test.go | 20 +++++----- 3 files changed, 49 insertions(+), 26 deletions(-) diff --git a/leetcode/1801-1900/1817.Finding-the-Users-Active-Minutes/README.md b/leetcode/1801-1900/1817.Finding-the-Users-Active-Minutes/README.md index 6cf46d386..cb91e7408 100755 --- a/leetcode/1801-1900/1817.Finding-the-Users-Active-Minutes/README.md +++ b/leetcode/1801-1900/1817.Finding-the-Users-Active-Minutes/README.md @@ -1,28 +1,38 @@ # [1817.Finding the Users Active Minutes][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 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`. + +**Multiple users** can perform actions simultaneously, and a single user can perform **multiple actions** in the same minute. + +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. + +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`. + +Return the array `answer` as described above. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: logs = [[0,5],[1,2],[0,2],[0,5],[1,3]], k = 5 +Output: [0,2,0,0,0] +Explanation: +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). +The user with ID=1 performed actions at minutes 2 and 3. Hence, they have a UAM of 2. +Since both users have a UAM of 2, answer[2] is 2, and the remaining answer[j] values are 0. ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Finding the Users Active Minutes -```go ``` - +Input: logs = [[1,1],[2,2],[2,3]], k = 4 +Output: [1,1,0,0] +Explanation: +The user with ID=1 performed a single action at minute 1. Hence, they have a UAM of 1. +The user with ID=2 performed actions at minutes 2 and 3. Hence, they have a UAM of 2. +There is one user with a UAM of 1 and one with a UAM of 2. +Hence, answer[1] = 1, answer[2] = 1, and the remaining values are 0. +``` ## 结语 diff --git a/leetcode/1801-1900/1817.Finding-the-Users-Active-Minutes/Solution.go b/leetcode/1801-1900/1817.Finding-the-Users-Active-Minutes/Solution.go index d115ccf5e..95c3c0643 100644 --- a/leetcode/1801-1900/1817.Finding-the-Users-Active-Minutes/Solution.go +++ b/leetcode/1801-1900/1817.Finding-the-Users-Active-Minutes/Solution.go @@ -1,5 +1,18 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(logs [][]int, k int) []int { + ret := make([]int, k) + // 1:1 + // 2:2 + count := make(map[int]map[int]struct{}) + for _, log := range logs { + if _, ok := count[log[0]]; !ok { + count[log[0]] = make(map[int]struct{}) + } + count[log[0]][log[1]] = struct{}{} + } + for _, c := range count { + ret[len(c)-1]++ + } + return ret } diff --git a/leetcode/1801-1900/1817.Finding-the-Users-Active-Minutes/Solution_test.go b/leetcode/1801-1900/1817.Finding-the-Users-Active-Minutes/Solution_test.go index 14ff50eb4..c41d6e30f 100644 --- a/leetcode/1801-1900/1817.Finding-the-Users-Active-Minutes/Solution_test.go +++ b/leetcode/1801-1900/1817.Finding-the-Users-Active-Minutes/Solution_test.go @@ -10,30 +10,30 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + logs [][]int + k int + expect []int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", [][]int{{0, 5}, {1, 2}, {0, 2}, {0, 5}, {1, 3}}, 5, []int{0, 2, 0, 0, 0}}, + {"TestCase2", [][]int{{1, 1}, {2, 2}, {2, 3}}, 4, []int{1, 1, 0, 0}}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.logs, c.k) 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.logs, c.k) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }