Skip to content

Commit 05b1bb0

Browse files
committed
feat: add solutions to lc problem: No.1348
No.1348.Tweet Counts Per Frequency
1 parent b41d0f8 commit 05b1bb0

File tree

5 files changed

+325
-0
lines changed

5 files changed

+325
-0
lines changed

solution/1300-1399/1348.Tweet Counts Per Frequency/README.md

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,22 +71,138 @@ tweetCounts.getTweetCountsPerFrequency("hour", "tweet3", 0, 210); // 返
7171

7272
<!-- 这里可写通用的实现逻辑 -->
7373

74+
**方法一:哈希表 + 有序列表**
75+
76+
我们用哈希表 `data` 记录每个用户的推文时间,用有序列表记录每个用户的所有推文时间。
77+
78+
对于 `recordTweet` 操作,我们将推文时间加入到用户的推文时间列表中。
79+
80+
对于 `getTweetCountsPerFrequency` 操作,我们先计算出时间间隔 `f`,然后遍历用户的推文时间列表,统计每个时间间隔内的推文数量。
81+
82+
时间复杂度,对于 `recordTweet` 操作,总的时间复杂度 $O(n \times \log n)$;对于 `getTweetCountsPerFrequency` 操作,总的时间复杂度 $O(q \times (t + \log n))$。其中 $n$, $q$ 和 $t$ 分别表示插入的推文数量,查询的次数和时间间隔的长度。
83+
7484
<!-- tabs:start -->
7585

7686
### **Python3**
7787

7888
<!-- 这里可写当前语言的特殊实现逻辑 -->
7989

8090
```python
91+
from sortedcontainers import SortedList
92+
8193

94+
class TweetCounts:
95+
96+
def __init__(self):
97+
self.d = {"minute": 60, "hour": 3600, "day": 86400}
98+
self.data = defaultdict(SortedList)
99+
100+
def recordTweet(self, tweetName: str, time: int) -> None:
101+
self.data[tweetName].add(time)
102+
103+
def getTweetCountsPerFrequency(self, freq: str, tweetName: str, startTime: int, endTime: int) -> List[int]:
104+
f = self.d[freq]
105+
tweets = self.data[tweetName]
106+
t = startTime
107+
ans = []
108+
while t <= endTime:
109+
l = tweets.bisect_left(t)
110+
r = tweets.bisect_left(min(t + f, endTime + 1))
111+
ans.append(r - l)
112+
t += f
113+
return ans
114+
115+
116+
# Your TweetCounts object will be instantiated and called as such:
117+
# obj = TweetCounts()
118+
# obj.recordTweet(tweetName,time)
119+
# param_2 = obj.getTweetCountsPerFrequency(freq,tweetName,startTime,endTime)
82120
```
83121

84122
### **Java**
85123

86124
<!-- 这里可写当前语言的特殊实现逻辑 -->
87125

88126
```java
127+
class TweetCounts {
128+
private Map<String, TreeMap<Integer, Integer>> data = new HashMap<>();
129+
130+
public TweetCounts() {
131+
132+
}
133+
134+
public void recordTweet(String tweetName, int time) {
135+
data.putIfAbsent(tweetName, new TreeMap<>());
136+
var tm = data.get(tweetName);
137+
tm.put(time, tm.getOrDefault(time, 0) + 1);
138+
}
139+
140+
public List<Integer> getTweetCountsPerFrequency(String freq, String tweetName, int startTime, int endTime) {
141+
int f = 60;
142+
if ("hour".equals(freq)) {
143+
f = 3600;
144+
} else if ("day".equals(freq)) {
145+
f = 86400;
146+
}
147+
var tm = data.get(tweetName);
148+
List<Integer> ans = new ArrayList<>();
149+
for (int i = startTime; i <= endTime; i += f) {
150+
int s = 0;
151+
int end = Math.min(i + f, endTime + 1);
152+
for (int v : tm.subMap(i, end).values()) {
153+
s += v;
154+
}
155+
ans.add(s);
156+
}
157+
return ans;
158+
}
159+
}
160+
161+
/**
162+
* Your TweetCounts object will be instantiated and called as such:
163+
* TweetCounts obj = new TweetCounts();
164+
* obj.recordTweet(tweetName,time);
165+
* List<Integer> param_2 = obj.getTweetCountsPerFrequency(freq,tweetName,startTime,endTime);
166+
*/
167+
```
89168

169+
### **C++**
170+
171+
```cpp
172+
class TweetCounts {
173+
public:
174+
TweetCounts() {
175+
}
176+
177+
void recordTweet(string tweetName, int time) {
178+
data[tweetName].insert(time);
179+
}
180+
181+
vector<int> getTweetCountsPerFrequency(string freq, string tweetName, int startTime, int endTime) {
182+
int f = 60;
183+
if (freq == "hour")
184+
f = 3600;
185+
else if (freq == "day")
186+
f = 86400;
187+
vector<int> ans((endTime - startTime) / f + 1);
188+
auto l = data[tweetName].lower_bound(startTime);
189+
auto r = data[tweetName].upper_bound(endTime);
190+
for (; l != r; ++l) {
191+
++ans[(*l - startTime) / f];
192+
}
193+
return ans;
194+
}
195+
196+
private:
197+
unordered_map<string, multiset<int>> data;
198+
};
199+
200+
/**
201+
* Your TweetCounts object will be instantiated and called as such:
202+
* TweetCounts* obj = new TweetCounts();
203+
* obj->recordTweet(tweetName,time);
204+
* vector<int> param_2 = obj->getTweetCountsPerFrequency(freq,tweetName,startTime,endTime);
205+
*/
90206
```
91207
92208
### **...**

solution/1300-1399/1348.Tweet Counts Per Frequency/README_EN.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,119 @@ tweetCounts.getTweetCountsPerFrequency(&quot;hour&quot;, &quot;tweet3&quot;, 0,
6868
### **Python3**
6969

7070
```python
71+
from sortedcontainers import SortedList
7172

73+
74+
class TweetCounts:
75+
76+
def __init__(self):
77+
self.d = {"minute": 60, "hour": 3600, "day": 86400}
78+
self.data = defaultdict(SortedList)
79+
80+
def recordTweet(self, tweetName: str, time: int) -> None:
81+
self.data[tweetName].add(time)
82+
83+
def getTweetCountsPerFrequency(self, freq: str, tweetName: str, startTime: int, endTime: int) -> List[int]:
84+
f = self.d[freq]
85+
tweets = self.data[tweetName]
86+
t = startTime
87+
ans = []
88+
while t <= endTime:
89+
l = tweets.bisect_left(t)
90+
r = tweets.bisect_left(min(t + f, endTime + 1))
91+
ans.append(r - l)
92+
t += f
93+
return ans
94+
95+
96+
# Your TweetCounts object will be instantiated and called as such:
97+
# obj = TweetCounts()
98+
# obj.recordTweet(tweetName,time)
99+
# param_2 = obj.getTweetCountsPerFrequency(freq,tweetName,startTime,endTime)
72100
```
73101

74102
### **Java**
75103

76104
```java
105+
class TweetCounts {
106+
private Map<String, TreeMap<Integer, Integer>> data = new HashMap<>();
107+
108+
public TweetCounts() {
109+
110+
}
111+
112+
public void recordTweet(String tweetName, int time) {
113+
data.putIfAbsent(tweetName, new TreeMap<>());
114+
var tm = data.get(tweetName);
115+
tm.put(time, tm.getOrDefault(time, 0) + 1);
116+
}
117+
118+
public List<Integer> getTweetCountsPerFrequency(String freq, String tweetName, int startTime, int endTime) {
119+
int f = 60;
120+
if ("hour".equals(freq)) {
121+
f = 3600;
122+
} else if ("day".equals(freq)) {
123+
f = 86400;
124+
}
125+
var tm = data.get(tweetName);
126+
List<Integer> ans = new ArrayList<>();
127+
for (int i = startTime; i <= endTime; i += f) {
128+
int s = 0;
129+
int end = Math.min(i + f, endTime + 1);
130+
for (int v : tm.subMap(i, end).values()) {
131+
s += v;
132+
}
133+
ans.add(s);
134+
}
135+
return ans;
136+
}
137+
}
138+
139+
/**
140+
* Your TweetCounts object will be instantiated and called as such:
141+
* TweetCounts obj = new TweetCounts();
142+
* obj.recordTweet(tweetName,time);
143+
* List<Integer> param_2 = obj.getTweetCountsPerFrequency(freq,tweetName,startTime,endTime);
144+
*/
145+
```
77146

147+
### **C++**
148+
149+
```cpp
150+
class TweetCounts {
151+
public:
152+
TweetCounts() {
153+
}
154+
155+
void recordTweet(string tweetName, int time) {
156+
data[tweetName].insert(time);
157+
}
158+
159+
vector<int> getTweetCountsPerFrequency(string freq, string tweetName, int startTime, int endTime) {
160+
int f = 60;
161+
if (freq == "hour")
162+
f = 3600;
163+
else if (freq == "day")
164+
f = 86400;
165+
vector<int> ans((endTime - startTime) / f + 1);
166+
auto l = data[tweetName].lower_bound(startTime);
167+
auto r = data[tweetName].upper_bound(endTime);
168+
for (; l != r; ++l) {
169+
++ans[(*l - startTime) / f];
170+
}
171+
return ans;
172+
}
173+
174+
private:
175+
unordered_map<string, multiset<int>> data;
176+
};
177+
178+
/**
179+
* Your TweetCounts object will be instantiated and called as such:
180+
* TweetCounts* obj = new TweetCounts();
181+
* obj->recordTweet(tweetName,time);
182+
* vector<int> param_2 = obj->getTweetCountsPerFrequency(freq,tweetName,startTime,endTime);
183+
*/
78184
```
79185
80186
### **...**
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class TweetCounts {
2+
public:
3+
TweetCounts() {
4+
}
5+
6+
void recordTweet(string tweetName, int time) {
7+
data[tweetName].insert(time);
8+
}
9+
10+
vector<int> getTweetCountsPerFrequency(string freq, string tweetName, int startTime, int endTime) {
11+
int f = 60;
12+
if (freq == "hour")
13+
f = 3600;
14+
else if (freq == "day")
15+
f = 86400;
16+
vector<int> ans((endTime - startTime) / f + 1);
17+
auto l = data[tweetName].lower_bound(startTime);
18+
auto r = data[tweetName].upper_bound(endTime);
19+
for (; l != r; ++l) {
20+
++ans[(*l - startTime) / f];
21+
}
22+
return ans;
23+
}
24+
25+
private:
26+
unordered_map<string, multiset<int>> data;
27+
};
28+
29+
/**
30+
* Your TweetCounts object will be instantiated and called as such:
31+
* TweetCounts* obj = new TweetCounts();
32+
* obj->recordTweet(tweetName,time);
33+
* vector<int> param_2 = obj->getTweetCountsPerFrequency(freq,tweetName,startTime,endTime);
34+
*/
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class TweetCounts {
2+
private Map<String, TreeMap<Integer, Integer>> data = new HashMap<>();
3+
4+
public TweetCounts() {
5+
6+
}
7+
8+
public void recordTweet(String tweetName, int time) {
9+
data.putIfAbsent(tweetName, new TreeMap<>());
10+
var tm = data.get(tweetName);
11+
tm.put(time, tm.getOrDefault(time, 0) + 1);
12+
}
13+
14+
public List<Integer> getTweetCountsPerFrequency(String freq, String tweetName, int startTime, int endTime) {
15+
int f = 60;
16+
if ("hour".equals(freq)) {
17+
f = 3600;
18+
} else if ("day".equals(freq)) {
19+
f = 86400;
20+
}
21+
var tm = data.get(tweetName);
22+
List<Integer> ans = new ArrayList<>();
23+
for (int i = startTime; i <= endTime; i += f) {
24+
int s = 0;
25+
int end = Math.min(i + f, endTime + 1);
26+
for (int v : tm.subMap(i, end).values()) {
27+
s += v;
28+
}
29+
ans.add(s);
30+
}
31+
return ans;
32+
}
33+
}
34+
35+
/**
36+
* Your TweetCounts object will be instantiated and called as such:
37+
* TweetCounts obj = new TweetCounts();
38+
* obj.recordTweet(tweetName,time);
39+
* List<Integer> param_2 = obj.getTweetCountsPerFrequency(freq,tweetName,startTime,endTime);
40+
*/
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from sortedcontainers import SortedList
2+
3+
4+
class TweetCounts:
5+
6+
def __init__(self):
7+
self.d = {"minute": 60, "hour": 3600, "day": 86400}
8+
self.data = defaultdict(SortedList)
9+
10+
def recordTweet(self, tweetName: str, time: int) -> None:
11+
self.data[tweetName].add(time)
12+
13+
def getTweetCountsPerFrequency(self, freq: str, tweetName: str, startTime: int, endTime: int) -> List[int]:
14+
f = self.d[freq]
15+
tweets = self.data[tweetName]
16+
t = startTime
17+
ans = []
18+
while t <= endTime:
19+
l = tweets.bisect_left(t)
20+
r = tweets.bisect_left(min(t + f, endTime + 1))
21+
ans.append(r - l)
22+
t += f
23+
return ans
24+
25+
26+
# Your TweetCounts object will be instantiated and called as such:
27+
# obj = TweetCounts()
28+
# obj.recordTweet(tweetName,time)
29+
# param_2 = obj.getTweetCountsPerFrequency(freq,tweetName,startTime,endTime)

0 commit comments

Comments
 (0)