You are given a list of log entries logs, where each entry is a pair [server_id, status] and status is either "error" or "success".
A server must be replaced every time it accumulates 3 consecutive errors. When a server is replaced, its consecutive error count is reset to 0. A "success" entry for a server also resets its consecutive error count to 0, but it does not count as a replacement.
Return the total number of server replacements needed across all log entries.
Function Signature
def count_replacements(logs: list[list[str]]) -> int:
Rules
-
Each entry is a list of two strings: the server id (a positive integer written as a string, such as
"1"
) and the status.
-
Entries are processed in the order given.
-
Consecutive error counts are kept per server. An entry for one server never changes another server's count, so errors on server
"1"
stay consecutive even when entries for other servers appear between them.
Constraints
-
1 <= logs.length <= 10^5
-
logs[i].length == 2
-
server_id
represents an integer from 1 to
10^9
-
status
is either
"error"
or
"success"
Examples
Input: logs = [["1","error"],["1","error"],["1","error"],["1","error"]]
Output: 1
Server 1 reaches 3 consecutive errors at index 2, which triggers one replacement and resets its count. The fourth error only brings the count back to 1, so no further replacement happens.
Input: logs = [["1","error"],["2","error"],["1","error"],["1","success"],["1","error"],["1","error"],["1","error"]]
Output: 1
Server 1 has 2 consecutive errors when the success at index 3 resets its count. After that it reaches 3 consecutive errors at index 6, which triggers one replacement. Server 2 never gets past 1 error.
Input: logs = [["1","error"],["2","error"],["1","error"],["2","error"],["1","error"],["2","success"]]
Output: 1
Server 1 errors at indices 0, 2 and 4. The entries for server 2 in between do not break its streak, so it is replaced once. Server 2 reaches 2 errors before its success resets the count.