-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #927 from 0xff-dev/1348
Add solution and test-cases for problem 1348
- Loading branch information
Showing
3 changed files
with
116 additions
and
25 deletions.
There are no files selected for viewing
50 changes: 34 additions & 16 deletions
50
leetcode/1301-1400/1348.Tweet-Counts-Per-Frequency/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 69 additions & 2 deletions
71
leetcode/1301-1400/1348.Tweet-Counts-Per-Frequency/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters