Problem Statement
You have a RecentCounter class which counts the number of recent requests within a certain time frame.
Implement the RecentCounter class:
- RecentCounter() Initializes the counter with zero recent requests.
- int ping(int t) Adds a new request at time t, where t represents some time in milliseconds, and returns the number of requests that has happened in the past 3000 milliseconds (including the new request). Specifically, return the number of requests that have happened in the inclusive range [t - 3000, t]. It is guaranteed that every call to ping uses a strictly larger value of t than the previous call.
class RecentCounter:
def __init__(self):
pass
def ping(self, t):
pass
# Note that the following code is for local testing purposes only. You should leave this part of code unchanged and not submit it to the OJ system.
T = int(input())
for t in range(1,T+1):
counter = RecentCounter()
calls = list(map(int,input().split()))
for ind,time in enumerate(calls):
print(f'Case #{t}_{ind}: {counter.ping(time)}')
Please note that you only have to submit the definition of the RecentCounter
and its content to the OJ system. The rest of the code is for your local testing purposes only.
Input Specification
The first line of the input contains a single integer T, the number of test cases.
For each test case, there is one line:
The line contains space-separated integers , representing the times at which the ping function is called.
These times are guaranteed to be strictly increasing.
Constraints
Output Specification
For each function call, you must return exactly what we asked for.
Sample Input
2
1 100 3001 3002
1 512 3001 3512 6001
Sample Output
Case #1_0: 1
Case #1_1: 2
Case #1_2: 3
Case #1_3: 3
Case #2_0: 1
Case #2_1: 2
Case #2_2: 3
Case #2_3: 3
Case #2_4: 3
Explaination
For Case#1, there are 4 function calls as follow
counter.ping(1) # requests = [1] range is [-2999,1] return 1
counter.ping(100) # requests = [1,100] range is [-2900,100], since there are two requests in the range so it will return 2
counter.ping(3001) # requests = [1,100,3001] range is [1,3001], since there are three requests in the range so it will return 3
counter.ping(3002) # requests = [1,100,3001,3002] range is [2,3002], since there are three requests in the range so it will return 3
For Case#2, there are 5 function calls as follow
counter.ping(1) # requests = [1] range is [-2999,1] return 1
counter.ping(512) # requests = [1,512] range is [-2488,512], since there are two requests in the range so it will return 2
counter.ping(3001) # requests = [1,512,3001] range is [1,3001], since there are three requests in the range so it will return 3
counter.ping(3512) # requests = [1,512,3001,3512] range is [512,3512], since there are three requests in the range so it will return 3
counter.ping(6001) # requests = [1,512,3001,3512,6001] range is [3001,6001], since there are three requests in the range so it will return 3
Hint
object oriented programming
Ref
https://leetcode.com/problems/number-of-recent-calls/submissions/940175645/
Comments
助教你好:
在Template code中的第12行 (calls = list(map(int,input.split())))
其中input似乎少加了括號(),在執行的時候會報錯,麻煩助教確認一下,謝謝!
同學你好,已經修正了,謝謝同學的提醒!