A service records its health in a log string, one character per time step: 1 means the service was running normally at that step and 0 means it had failed. You may perform at most one maintenance operation. The operation selects one contiguous block of exactly k consecutive positions of the log and changes every 0 inside the block to 1; positions that are already 1 stay 1. Choose the block, or skip the operation, so that the longest run of consecutive 1s in the resulting log is as long as possible, and return that length.
The block always covers exactly k adjacent positions. This is not the same as changing any k zeros of your choice: two zeros that are far apart cannot both be repaired by a single operation.
Function Signature
def longest_uptime_after_maintenance(log: str, k: int) -> int:
Rules
-
log[i]
is the status at time step
i
(0-indexed).
-
If the operation is performed, it chooses a start index
s
with
0 <= s <= len(log) - k
and sets positions
s
through
s + k - 1
to
1
.
-
The result is the length of the longest block of consecutive
1
s in the log after the optional operation, returned as a single integer.
Constraints
-
1 <= len(log) <= 10^5
-
1 <= k <= len(log)
-
Every character of
log
is
'0'
or
'1'
.
Examples
Example 1
Input: log = "1001110", k = 2
Output: 6
Repairing positions 1 and 2 gives "1111110", whose first six steps are all 1.
Example 2
Input: log = "1010101", k = 2
Output: 3
Any block of length 2 covers two adjacent positions and so repairs only one of the zeros; for example, positions 1 and 2 give "1110101". No choice of block produces a run longer than 3. Repairing the zeros at positions 1 and 3 together would give a run of 5, but they do not fit inside one block of length 2.
Example 3
Input: log = "111", k = 2
Output: 3
The log is already all 1, and any block leaves it unchanged.