列表

详情


1348. 推文计数

一家社交媒体公司正试图通过分析特定时间段内出现的推文数量来监控其网站上的活动。这些时间段可以根据特定的频率( 每分钟 每小时 每一天 )划分为更小的 时间段

 

例如,周期 [10,10000] (以 为单位)将被划分为以下频率的 时间块 :

注意,最后一个块可能比指定频率的块大小更短,并且总是以时间段的结束时间结束(在上面的示例中为 10000 )。

设计和实现一个API来帮助公司进行分析。

实现 TweetCounts 类:

 

示例:

输入:
["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]]

输出:
[null,null,null,null,[2],[2,1],null,[4]]

解释:
TweetCounts tweetCounts = new TweetCounts();
tweetCounts.recordTweet("tweet3", 0);
tweetCounts.recordTweet("tweet3", 60);
tweetCounts.recordTweet("tweet3", 10);                             // "tweet3" 发布推文的时间分别是 0, 10 和 60 。
tweetCounts.getTweetCountsPerFrequency("minute", "tweet3", 0, 59); // 返回 [2]。统计频率是每分钟(60 秒),因此只有一个有效时间间隔 [0,60> - > 2 条推文。
tweetCounts.getTweetCountsPerFrequency("minute", "tweet3", 0, 60); // 返回 [2,1]。统计频率是每分钟(60 秒),因此有两个有效时间间隔 1) [0,60> - > 2 条推文,和 2) [60,61> - > 1 条推文。 
tweetCounts.recordTweet("tweet3", 120);                            // "tweet3" 发布推文的时间分别是 0, 10, 60 和 120 。
tweetCounts.getTweetCountsPerFrequency("hour", "tweet3", 0, 210);  // 返回 [4]。统计频率是每小时(3600 秒),因此只有一个有效时间间隔 [0,211> - > 4 条推文。

 

提示:

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
class TweetCounts { public: TweetCounts() { } void recordTweet(string tweetName, int time) { } vector<int> getTweetCountsPerFrequency(string freq, string tweetName, int startTime, int endTime) { } }; /** * 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); */

golang 解法, 执行用时: 196 ms, 内存消耗: 9.7 MB, 提交时间: 2023-09-11 11:39:05

import "sort"

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

func Constructor() TweetCounts {
	tc := TweetCounts{
		tweets: map[string][]int{},
	}
	return tc
}

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

func (this *TweetCounts) GetTweetCountsPerFrequency(freq string, tweetName string, startTime int, endTime int) (rst []int) {
	sort.Ints(this.tweets[tweetName])
	var freqSecond = 60
	switch freq {
	case "minute":
		break
	case "hour":
		freqSecond = 3600
	case "day":
		freqSecond = 24 * 3600
	}

	var tweets = this.tweets[tweetName]
	rst = make([]int, (endTime-startTime)/freqSecond+1)
	for i := 0; i < len(tweets); i++ {
		if tweets[i] < startTime {
			continue
		}
		if tweets[i] > endTime {
			return rst

		}
		rst[(tweets[i]-startTime)/freqSecond]++
	}
	return rst
}


/**
 * Your TweetCounts object will be instantiated and called as such:
 * obj := Constructor();
 * obj.RecordTweet(tweetName,time);
 * param_2 := obj.GetTweetCountsPerFrequency(freq,tweetName,startTime,endTime);
 */

cpp 解法, 执行用时: 92 ms, 内存消耗: 37.1 MB, 提交时间: 2023-09-11 11:36:36

class TweetCounts {
    map<string, vector<int>> user;
public:
    TweetCounts() {
    }
    
    void recordTweet(string tweetName, int time) {
        user[tweetName].push_back(time);
    }
    
    vector<int> getTweetCountsPerFrequency(string freq, string tweetName, int startTime, int endTime) {
        endTime += 1;
        int length = 0;
        if (freq == "minute")
            length = 60;
        else if (freq == "hour")
            length = 60 * 60;
        else
            length = 60 * 60 * 24;
        vector<int> ans((endTime - startTime - 1) / length + 1);
        for (int time : user[tweetName])
            if (time >= startTime && time < endTime)
                ++ans[(time - startTime) / length];
        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);
 */

python3 解法, 执行用时: 416 ms, 内存消耗: 22.4 MB, 提交时间: 2023-09-11 11:36:17

'''
方法一:线性表暴力
使用字典保存用户,按照用户名使用线性表存储其相应的所有推文,
查询时遍历该用户的所有推文判断是否在要求的时间区间内。
'''
class TweetCounts:

    def __init__(self):
        self.user = collections.defaultdict(list)

    def recordTweet(self, tweetName: str, time: int) -> None:
        self.user[tweetName].append(time)

    def getTweetCountsPerFrequency(self, freq: str, tweetName: str, startTime: int, endTime: int) -> List[int]:
        endTime += 1
        if freq == 'minute':
            length = 60
        elif freq == 'hour':
            length = 60 * 60
        else:
            length = 60 * 60 * 24
        ans = [0] * ((endTime - startTime - 1) // length + 1)
        for t in self.user[tweetName]:
            if endTime > t >= startTime:
                ans[(t - startTime) // length] += 1
        return ans

# Your TweetCounts object will be instantiated and called as such:
# obj = TweetCounts()
# obj.recordTweet(tweetName,time)
# param_2 = obj.getTweetCountsPerFrequency(freq,tweetName,startTime,endTime)

上一题