Skip to content

Commit

Permalink
Merge pull request #927 from 0xff-dev/1348
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 1348
  • Loading branch information
6boris authored Sep 9, 2024
2 parents bba0c1d + 8f7cafc commit b4ae9e9
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 25 deletions.
50 changes: 34 additions & 16 deletions leetcode/1301-1400/1348.Tweet-Counts-Per-Frequency/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,46 @@
# [1348.Tweet Counts Per Frequency][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
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**).

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

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

## 题意
> ...
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).

## 题解
Design and implement an API to help the company with their analysis.

### 思路1
> ...
Tweet Counts Per Frequency
```go
```
Implement the `TweetCounts` class:

- `TweetCounts()` Initializes the `TweetCounts` object.
- `void recordTweet(String tweetName, int time)` Stores the `tweetName` at the recorded time (in **seconds**).
- `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`.

- `freq` is one of `"minute"`, `"hour"`, or `"day"` representing a frequency of every **minute**, **hour**, or **day** respectively.

**Example 1:**

```
Input
["TweetCounts","recordTweet","recordTweet","recordTweet","getTweetCountsPerFrequency","getTweetCountsPerFrequency","recordTweet","getTweetCountsPerFrequency"]
[[],["tweet3",0],["tweet3",60],["tweet3",10],["minute","tweet3",0,59],["minute","tweet3",0,60],["tweet3",120],["hour","tweet3",0,210]]
Output
[null,null,null,null,[2],[2,1],null,[4]]
Explanation
TweetCounts tweetCounts = new TweetCounts();
tweetCounts.recordTweet("tweet3", 0); // New tweet "tweet3" at time 0
tweetCounts.recordTweet("tweet3", 60); // New tweet "tweet3" at time 60
tweetCounts.recordTweet("tweet3", 10); // New tweet "tweet3" at time 10
tweetCounts.getTweetCountsPerFrequency("minute", "tweet3", 0, 59); // return [2]; chunk [0,59] had 2 tweets
tweetCounts.getTweetCountsPerFrequency("minute", "tweet3", 0, 60); // return [2,1]; chunk [0,59] had 2 tweets, chunk [60,60] had 1 tweet
tweetCounts.recordTweet("tweet3", 120); // New tweet "tweet3" at time 120
tweetCounts.getTweetCountsPerFrequency("hour", "tweet3", 0, 210); // return [4]; chunk [0,210] had 4 tweets
```

## 结语

Expand Down
71 changes: 69 additions & 2 deletions leetcode/1301-1400/1348.Tweet-Counts-Per-Frequency/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,72 @@
package Solution

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

type TweetCounts struct {
data map[string][]int
}

func Constructor() TweetCounts {
t := TweetCounts{
data: make(map[string][]int),
}
return t
}

func (this *TweetCounts) RecordTweet(tweetName string, time int) {
this.data[tweetName] = append(this.data[tweetName], time)
sort.Ints(this.data[tweetName])
}

func (this *TweetCounts) GetTweetCountsPerFrequency(freq string, tweetName string, startTime int, endTime int) []int {
interval := 60
if freq == "hour" {
interval = 3600
}
if freq == "day" {
interval = 86400
}

s := startTime
ans := make([]int, 0)
data := this.data[tweetName]
for s <= endTime {
e := s + interval - 1
e = min(endTime, e)
l := sort.Search(len(data), func(i int) bool {
return data[i] >= s
})
c := 0
if l != len(data) {
r := sort.Search(len(data), func(i int) bool {
return data[i] > e
})
c = r - l
}
ans = append(ans, c)
s = s + interval
}
return ans
}

type opt struct {
name string
tname string
cname string

ttime int
s, e int
}

func Solution(opts []opt) [][]int {
ans := make([][]int, 0)
c := Constructor()
for _, o := range opts {
if o.name == "r" {
c.RecordTweet(o.tname, o.ttime)
continue
}
ans = append(ans, c.GetTweetCountsPerFrequency(o.cname, o.tname, o.s, o.e))
}
return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs []opt
expect [][]int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []opt{
{"r", "tweet3", "", 0, 0, 0},
{"r", "tweet3", "", 60, 0, 0},
{"r", "tweet3", "", 10, 0, 0},
{"", "tweet3", "minute", 0, 0, 59},
{"", "tweet3", "minute", 0, 0, 60},
{"r", "tweet3", "", 120, 0, 0},
{"", "tweet3", "hour", 0, 0, 210},
}, [][]int{{2}, {2, 1}, {4}}},
}

// 开始测试
Expand All @@ -30,10 +36,10 @@ func TestSolution(t *testing.T) {
}
}

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

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

0 comments on commit b4ae9e9

Please sign in to comment.