-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsolution.js
59 lines (47 loc) · 1.18 KB
/
solution.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const sortedIndex = require('lodash/sortedIndex')
const intervalMap = {
minute: 60,
hour: 3600,
day: 86400
}
const TweetCounts = function () {
this.times = []
this.tweets = []
}
/**
* @param {string} tweetName
* @param {number} time
* @return {void}
*/
TweetCounts.prototype.recordTweet = function (tweetName, time) {
const index = sortedIndex(this.times, time)
this.times.splice(index, 0, time)
this.tweets.splice(index, 0, tweetName)
}
/**
* @param {string} freq
* @param {string} tweetName
* @param {number} startTime
* @param {number} endTime
* @return {number[]}
*/
TweetCounts.prototype.getTweetCountsPerFrequency = function (freq, tweetName, startTime, endTime) {
const result = []
const interval = intervalMap[freq]
let start = startTime
while (start <= endTime) {
const end = Math.min(start + interval, endTime + 1)
const startIndex = sortedIndex(this.times, start)
const endIndex = sortedIndex(this.times, end)
let count = 0
for (let i = startIndex; i < endIndex; i++) {
if (this.tweets[i] === tweetName) {
count += 1
}
}
result.push(count)
start = end
}
return result
}
module.exports = TweetCounts