Skip to content

Commit b4ae9e9

Browse files
authored
Merge pull request #927 from 0xff-dev/1348
Add solution and test-cases for problem 1348
2 parents bba0c1d + 8f7cafc commit b4ae9e9

File tree

3 files changed

+116
-25
lines changed

3 files changed

+116
-25
lines changed

leetcode/1301-1400/1348.Tweet-Counts-Per-Frequency/README.md

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,46 @@
11
# [1348.Tweet Counts Per Frequency][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+
A social media company is trying to monitor activity on their site by analyzing the number of tweets that occur in select periods of time. These periods can be partitioned into smaller **time chunks** based on a certain frequency (every **minute**, **hour**, or **day**).
75

8-
**Example 1:**
6+
For example, the period `[10, 10000]` (in **seconds**) would be partitioned into the following **time chunks** with these frequencies:
97

10-
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
8+
- Every **minute** (60-second chunks): `[10,69]`, `[70,129]`, `[130,189]`, ..., `[9970,10000]`
9+
- Every **hour** (3600-second chunks): `[10,3609]`, `[3610,7209]`, `[7210,10000]`
10+
- Every **day** (86400-second chunks): `[10,10000]`
1411

15-
## 题意
16-
> ...
12+
Notice that the last chunk may be shorter than the specified frequency's chunk size and will always end with the end time of the period (`10000` in the above example).
1713

18-
## 题解
14+
Design and implement an API to help the company with their analysis.
1915

20-
### 思路1
21-
> ...
22-
Tweet Counts Per Frequency
23-
```go
24-
```
16+
Implement the `TweetCounts` class:
17+
18+
- `TweetCounts()` Initializes the `TweetCounts` object.
19+
- `void recordTweet(String tweetName, int time)` Stores the `tweetName` at the recorded time (in **seconds**).
20+
- `List<Integer> getTweetCountsPerFrequency(String freq, String tweetName, int startTime, int endTime)` Returns a list of integers representing the number of tweets with `tweetName` in each **time chunk** for the given period of time `[startTime, endTime]` (in **seconds**) and frequency `freq`.
2521

22+
- `freq` is one of `"minute"`, `"hour"`, or `"day"` representing a frequency of every **minute**, **hour**, or **day** respectively.
23+
24+
**Example 1:**
25+
26+
```
27+
Input
28+
["TweetCounts","recordTweet","recordTweet","recordTweet","getTweetCountsPerFrequency","getTweetCountsPerFrequency","recordTweet","getTweetCountsPerFrequency"]
29+
[[],["tweet3",0],["tweet3",60],["tweet3",10],["minute","tweet3",0,59],["minute","tweet3",0,60],["tweet3",120],["hour","tweet3",0,210]]
30+
31+
Output
32+
[null,null,null,null,[2],[2,1],null,[4]]
33+
34+
Explanation
35+
TweetCounts tweetCounts = new TweetCounts();
36+
tweetCounts.recordTweet("tweet3", 0); // New tweet "tweet3" at time 0
37+
tweetCounts.recordTweet("tweet3", 60); // New tweet "tweet3" at time 60
38+
tweetCounts.recordTweet("tweet3", 10); // New tweet "tweet3" at time 10
39+
tweetCounts.getTweetCountsPerFrequency("minute", "tweet3", 0, 59); // return [2]; chunk [0,59] had 2 tweets
40+
tweetCounts.getTweetCountsPerFrequency("minute", "tweet3", 0, 60); // return [2,1]; chunk [0,59] had 2 tweets, chunk [60,60] had 1 tweet
41+
tweetCounts.recordTweet("tweet3", 120); // New tweet "tweet3" at time 120
42+
tweetCounts.getTweetCountsPerFrequency("hour", "tweet3", 0, 210); // return [4]; chunk [0,210] had 4 tweets
43+
```
2644

2745
## 结语
2846

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

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
type TweetCounts struct {
6+
data map[string][]int
7+
}
8+
9+
func Constructor() TweetCounts {
10+
t := TweetCounts{
11+
data: make(map[string][]int),
12+
}
13+
return t
14+
}
15+
16+
func (this *TweetCounts) RecordTweet(tweetName string, time int) {
17+
this.data[tweetName] = append(this.data[tweetName], time)
18+
sort.Ints(this.data[tweetName])
19+
}
20+
21+
func (this *TweetCounts) GetTweetCountsPerFrequency(freq string, tweetName string, startTime int, endTime int) []int {
22+
interval := 60
23+
if freq == "hour" {
24+
interval = 3600
25+
}
26+
if freq == "day" {
27+
interval = 86400
28+
}
29+
30+
s := startTime
31+
ans := make([]int, 0)
32+
data := this.data[tweetName]
33+
for s <= endTime {
34+
e := s + interval - 1
35+
e = min(endTime, e)
36+
l := sort.Search(len(data), func(i int) bool {
37+
return data[i] >= s
38+
})
39+
c := 0
40+
if l != len(data) {
41+
r := sort.Search(len(data), func(i int) bool {
42+
return data[i] > e
43+
})
44+
c = r - l
45+
}
46+
ans = append(ans, c)
47+
s = s + interval
48+
}
49+
return ans
50+
}
51+
52+
type opt struct {
53+
name string
54+
tname string
55+
cname string
56+
57+
ttime int
58+
s, e int
59+
}
60+
61+
func Solution(opts []opt) [][]int {
62+
ans := make([][]int, 0)
63+
c := Constructor()
64+
for _, o := range opts {
65+
if o.name == "r" {
66+
c.RecordTweet(o.tname, o.ttime)
67+
continue
68+
}
69+
ans = append(ans, c.GetTweetCountsPerFrequency(o.cname, o.tname, o.s, o.e))
70+
}
71+
return ans
572
}

leetcode/1301-1400/1348.Tweet-Counts-Per-Frequency/Solution_test.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,18 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []opt
14+
expect [][]int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []opt{
17+
{"r", "tweet3", "", 0, 0, 0},
18+
{"r", "tweet3", "", 60, 0, 0},
19+
{"r", "tweet3", "", 10, 0, 0},
20+
{"", "tweet3", "minute", 0, 0, 59},
21+
{"", "tweet3", "minute", 0, 0, 60},
22+
{"r", "tweet3", "", 120, 0, 0},
23+
{"", "tweet3", "hour", 0, 0, 210},
24+
}, [][]int{{2}, {2, 1}, {4}}},
1925
}
2026

2127
// 开始测试
@@ -30,10 +36,10 @@ func TestSolution(t *testing.T) {
3036
}
3137
}
3238

33-
// 压力测试
39+
// 压力测试
3440
func BenchmarkSolution(b *testing.B) {
3541
}
3642

37-
// 使用案列
43+
// 使用案列
3844
func ExampleSolution() {
3945
}

0 commit comments

Comments
 (0)