Calculate Server Shutdown Penalty
Problem statement
A server log contains one state per time slot. The states are separated by whitespace:
0means the server was up and functioning during that slot.1means the server was down during that slot.
You remove the server immediately before slot removedTime. Slots with indexes smaller than removedTime occur while the server is still installed; later slots occur after removal.
The penalty is the number of down slots before removal plus the number of up slots at or after removal. Return that total penalty.
Function
calculateShutdownPenalty(logs: String, removedTime: int) → intExamples
Example 1
logs = "0 0 0 1 0 0"removedTime = 3return = 2No down slot occurs before removal. Two up slots occur after it, so the penalty is 2.
Example 2
logs = "1 1 0 0"removedTime = 2return = 4The two down slots before removal and the two up slots after removal each contribute one penalty point.
Example 3
logs = "0 1 1"removedTime = 0return = 1The server is removed before every slot. Only the first slot was an up slot, so only it is penalized.
Constraints
1 <= n <= 200000, wherenis the number of log states.- Every state is exactly
0or1, separated by one or more whitespace characters. 0 <= removedTime <= n.