Problem · Hash Table
Count Server Replacements
Learn this problemProblem statement
You have n servers with IDs "s1", "s2", ..., "sn". The system processes a sequence of log entries, where each entry is formatted as "<server_id> <status>", and status is either "success" or "error".
For each server, track its consecutive errors:
- If a server records three
"error"logs in a row, it is considered faulty and is replaced. The replacement server keeps the same ID. - After a replacement, that server's consecutive error count resets to
0. - A
"success"log also resets that server's consecutive error count to0.
Determine the total number of server replacements that occur while processing all log entries.
Custom testing format
- The first line contains the integer
n. - The next line contains the integer
m, the size oflogs. - Each of the next
mlines contains one string element oflogs.
Function
countFaults(n: int, logs: String[]) → intExamples
Example 1
n = 2logs = ["s1 error", "s1 error", "s2 error", "s1 error", "s1 error", "s2 success"]return = 1- Server
s1logs its first error:[error]. - Server
s1logs its second error:[error, error]. - Server
s2logs its first error:[error]. - Server
s1logs its third consecutive error:[error, error, error], so it is replaced. - The new server
s1logs its first error:[error]. - Server
s2logs a success, so its consecutive-error record resets.
Only server s1 is replaced, and it is replaced once.
Constraints
1 <= n <= 2001 <= logs.length <= 2 * 10^4