Stopping comment spam
Write code · Challenge · §5.2 Queues
times holds, in order, the times (in seconds) at which a user wrote comments. To stop spam, a comment written at second t is blocked instead of being posted if k or more comments have already been posted from second t - 59 to second t (both included). Comments with the same time are written one at a time, in the order they appear in times. Blocked comments do not count as posted comments.
For example, a comment posted at second 0 counts toward the limit for comments written up to second 59, but not for comments written at second 60 or later. Complete the function solution, which returns the number of blocked comments.
Function
solution(times, k)
Constraints
- The length of
timesis from 1 to 200,000. - Each element of
timesis an integer from 0 to 1,000,000,000. The elements are in ascending order, and the same value may repeat. kis an integer from 1 to 200,000.
Examples
| times | k | result |
|---|---|---|
[1, 2, 3, 4, 5] | 3 | 2 |
[0, 30, 50, 100] | 2 | 1 |
Starter code
def solution(times, k):
answer = 0
return answerSolve this problem in your browser
Python runs in your browser and checks your answer right away. Hints and the answer are on the problem page.