Created
November 18, 2019 21:36
-
-
Save shanecowherd/1806e495bec075217a14741cbe655149 to your computer and use it in GitHub Desktop.
Number Of Recent Calls
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
//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