mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 16:36:41 +08:00
29 lines
533 B
Go
29 lines
533 B
Go
package leetcode
|
|
|
|
import "sort"
|
|
|
|
type RecentCounter struct {
|
|
list []int
|
|
}
|
|
|
|
func Constructor933() RecentCounter {
|
|
return RecentCounter{
|
|
list: []int{},
|
|
}
|
|
}
|
|
|
|
func (this *RecentCounter) Ping(t int) int {
|
|
this.list = append(this.list, t)
|
|
index := sort.Search(len(this.list), func(i int) bool { return this.list[i] >= t-3000 })
|
|
if index < 0 {
|
|
index = -index - 1
|
|
}
|
|
return len(this.list) - index
|
|
}
|
|
|
|
/**
|
|
* Your RecentCounter object will be instantiated and called as such:
|
|
* obj := Constructor();
|
|
* param_1 := obj.Ping(t);
|
|
*/
|