NewThe Learning module is now liveSee what's new

A company has a cluster of nn servers. Some are in the "OFF" state while others are "ON". The developers responsible for maintaining the servers have access to a special operation to change the states. In each operation, they can choose a contiguous sequence of servers and flip their states, i.e., "ON" to "OFF" and vice versa. Due to operational constraints, this operation can be performed on the cluster a maximum of k times.

Given a binary string serverStates of length nn, where '1' represents "ON", '0' represents "OFF", and an integer k. Return the maximum possible number of consecutive "ON" servers after at most k operations.

Constraints:

  • 0 ≤ k ≤ 100
  • 1 ≤ serverStates.length ≤ 10510^5

Example 1:

Input: serverStates = "00010", k = 1 Output: 4 Explanation: It is optimal to apply the special operations on consecutive indices (0-based indexing) as follows: - Flip indices from 0 to 2 giving states = "11110". - After 1 operation, there are a maximum of 4 consecutive "ON" servers.

Example 2:

Input: serverStates = "1001", k = 2 Output: 4 Explanation: It is optimal to apply the special operations on consecutive indices (0-based indexing) as follows: - Flip indices from 1 to 2, giving states = "1111". - After 1 operation, there are a maximum of 4 consecutive "ON" servers.

Example 3:

Input: serverStates = "11101010110011", k = 2 Output: 8 Explanation: It is optimal to apply the special operations on consecutive indices (0-based indexing) as follows: - Flip indices from 7 to 9 given states = "11101011000011" - Flip indices from 8 to 11 given states = "11101011111111" - After 2 operations, a maximum of 8 consecutive "ON" servers can be obtained.


Accepted
751/2.1k
Acceptance Rate36.1%
Test Cases
Input
serverStates =
k =
Output
Expected