Skip to content

Instantly share code, notes, and snippets.

@shanecowherd
Created November 18, 2019 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shanecowherd/1806e495bec075217a14741cbe655149 to your computer and use it in GitHub Desktop.
Save shanecowherd/1806e495bec075217a14741cbe655149 to your computer and use it in GitHub Desktop.
Number Of Recent Calls
//https://leetcode.com/problems/number-of-recent-calls/
// Tried for loop, etc. While loop was the best when dealing with 10000 requests
// Write a class RecentCounter to count recent requests.
// It has only one method: ping(int t), where t represents some time in milliseconds.
// Return the number of pings that have been made from 3000 milliseconds ago until now.
// Any ping with time in [t - 3000, t] will count, including the current ping.
// It is guaranteed that every call to ping uses a strictly larger value of t than before.
class RecentCounter {
var pings = [Int]()
var threesecondsago = 0
init() {
}
func ping(_ t: Int) -> Int {
threesecondsago = t - 3000
pings.append(t)
while pings[0] < threesecondsago {
pings.remove(at: 0)
}
return pings.count
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment