A server's health log is a string of 0s and 1s, one character per time step: 1 means the server was up and running normally, and 0 means it had failed. You may run at most one maintenance operation, which picks a contiguous stretch of exactly k consecutive entries and sets every 0 in that stretch to 1 (entries that are already 1 are unchanged). Return the greatest possible length of a run of consecutive 1s in the log after you choose the stretch optimally, or decide not to run the operation.
The stretch length is fixed at exactly k. The operation does not let you flip up to k arbitrary zeros; it repairs only the zeros inside one window of k adjacent entries.
Function Signature
def max_uptime_after_one_repair(log: str, k: int) -> int:
Rules
-
Positions are 0-indexed. A repair starting at index
s
must satisfy
0 <= s
and
s + k <= len(log)
, and it covers positions
s, s + 1, ..., s + k - 1
.
-
At most one repair is allowed.
-
Return one integer: the length of the longest run of consecutive
1
s in the final log.
Constraints
-
1 <= len(log) <= 10^5
-
1 <= k <= len(log)
-
log
contains only the characters
'0'
and
'1'
.
Examples
Example 1
Input: log = "0110100", k = 3
Output: 5
Repairing positions 3 to 5 gives "0111110", with a run of five 1s at positions 1 to 5.
Example 2
Input: log = "01010", k = 2
Output: 3
The best windows are positions 1 to 2 or positions 2 to 3; both give "01110". Fixing the zeros at positions 0 and 2 would give "11110" with a run of 4, but those two zeros do not lie inside one window of length 2, so that is not allowed.
Example 3
Input: log = "0000", k = 1
Output: 1
A window of length 1 repairs a single entry, producing one isolated 1.