What is the frequency distribution of tweet counts in Leetcode 1348 problem?

2026-05-29 13:553阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计460个文字,预计阅读时间需要2分钟。

What is the frequency distribution of tweet counts in Leetcode 1348 problem?

题目链接:Tweet Counts Per Frequency题目大意:设定一些功能用于统计鸟鸣声的数量,recordTweet(string tweetName, int time)表示在time秒时某个鸟鸣声出现了,getTweetCountsPerFrequency(string freq)返回指定频率下的鸟鸣声统计结果。


题目链接:​​Tweet Counts Per Frequency​​

What is the frequency distribution of tweet counts in Leetcode 1348 problem?

题目大意:给定一些功能用于统计鸟鸣数, recordTweet(string tweetName, int time)代表在time秒某个鸣叫声出现了一次,getTweetCountsPerFrequency(string freq, string tweetName, int startTime, int endTime)要求你去做统计,统计从startTime到endTime这段时间里时间间隔为每freq的鸣叫数,比如统计第1秒到68秒的鸣叫声,要求以分钟做间隔,也就是1到60秒,60秒到68秒,每个时间间隔的鸣叫数

题目思路:算是一个模拟题,不过处理起来稍微有点麻烦,我们不能统计时间段内每个鸣叫的次数,这样统计起来太麻烦了,所以我们选择倒着判断,我们开一个vector记录下来所有鸣叫的地方,每次只需要判断每个鸣叫数属于哪个区间即可,这样就可以剩下统计的时间

时间复杂度&&空间复杂度:q代表查询次数,p代表添加次数,O(q*p)&&O(p+q)

class TweetCounts {
public:
map<string,vector<int> >mp;

TweetCounts() {
mp.clear();
}

void recordTweet(string tweetName, int time) {
mp[tweetName].push_back(time);
}

vector<int> getTweetCountsPerFrequency(string freq, string tweetName, int startTime, int endTime) {
int delta = 60;
if(freq == "hour") delta = 3600;
if(freq == "day") delta = 3600*24;
int interval = (endTime - startTime)/delta+1;
vector<int>ans(interval);
int len = mp[tweetName].size();
for(int j = 0;j < len;j++){
if(mp[tweetName][j] < startTime||mp[tweetName][j] > endTime) continue;
ans[(mp[tweetName][j]-startTime)/delta]++;
}
return ans;
}
};

/**
* Your TweetCounts object will be instantiated and called as such:
* TweetCounts* obj = new TweetCounts();
* obj->recordTweet(tweetName,time);
* vector<int> param_2 = obj->getTweetCountsPerFrequency(freq,tweetName,startTime,endTime);
*/


本文共计460个文字,预计阅读时间需要2分钟。

What is the frequency distribution of tweet counts in Leetcode 1348 problem?

题目链接:Tweet Counts Per Frequency题目大意:设定一些功能用于统计鸟鸣声的数量,recordTweet(string tweetName, int time)表示在time秒时某个鸟鸣声出现了,getTweetCountsPerFrequency(string freq)返回指定频率下的鸟鸣声统计结果。


题目链接:​​Tweet Counts Per Frequency​​

What is the frequency distribution of tweet counts in Leetcode 1348 problem?

题目大意:给定一些功能用于统计鸟鸣数, recordTweet(string tweetName, int time)代表在time秒某个鸣叫声出现了一次,getTweetCountsPerFrequency(string freq, string tweetName, int startTime, int endTime)要求你去做统计,统计从startTime到endTime这段时间里时间间隔为每freq的鸣叫数,比如统计第1秒到68秒的鸣叫声,要求以分钟做间隔,也就是1到60秒,60秒到68秒,每个时间间隔的鸣叫数

题目思路:算是一个模拟题,不过处理起来稍微有点麻烦,我们不能统计时间段内每个鸣叫的次数,这样统计起来太麻烦了,所以我们选择倒着判断,我们开一个vector记录下来所有鸣叫的地方,每次只需要判断每个鸣叫数属于哪个区间即可,这样就可以剩下统计的时间

时间复杂度&&空间复杂度:q代表查询次数,p代表添加次数,O(q*p)&&O(p+q)

class TweetCounts {
public:
map<string,vector<int> >mp;

TweetCounts() {
mp.clear();
}

void recordTweet(string tweetName, int time) {
mp[tweetName].push_back(time);
}

vector<int> getTweetCountsPerFrequency(string freq, string tweetName, int startTime, int endTime) {
int delta = 60;
if(freq == "hour") delta = 3600;
if(freq == "day") delta = 3600*24;
int interval = (endTime - startTime)/delta+1;
vector<int>ans(interval);
int len = mp[tweetName].size();
for(int j = 0;j < len;j++){
if(mp[tweetName][j] < startTime||mp[tweetName][j] > endTime) continue;
ans[(mp[tweetName][j]-startTime)/delta]++;
}
return ans;
}
};

/**
* Your TweetCounts object will be instantiated and called as such:
* TweetCounts* obj = new TweetCounts();
* obj->recordTweet(tweetName,time);
* vector<int> param_2 = obj->getTweetCountsPerFrequency(freq,tweetName,startTime,endTime);
*/