3228. Maximum Number of Operations to Move Ones to the End
Difficulty: Medium
Topics: String, Greedy, Counting, Weekly Contest 407
You are given a binary string s.
You can perform the following operation on the string any number of times:
- Choose any index
ifrom the string wherei + 1 < s.lengthsuch thats[i] == '1'ands[i + 1] == '0'. - Move the character
s[i]to the right until it reaches the end of the string or another'1'. For example, fors = "010010", if we choosei = 1, the resulting string will bes = "000110".
Return the maximum number of operations that you can perform.
Example 1:
- Input: s = "1001101"
- Output: 4
-
Explanation: We can perform the following operations:
- Choose index
i = 0. The resulting string iss = "001101". - Choose index
i = 4. The resulting string iss = "0011011". - Choose index
i = 3. The resulting string iss = "0010111". - Choose index
i = 2. The resulting string iss = "0001111".
- Choose index
Example 2:
- Input: s = "00111"
- Output: 0
Constraints:
1 <= s.length <= 10⁵-
s[i]is either'0'or'1'.
Hint:
- It is optimal to perform the operation on the lowest index possible each time.
- Traverse the string from left to right and perform the operation every time it is possible.
Solution:
We need to determine the maximum number of operations that can be performed on a binary string. Each operation involves moving a '1' to the right until it reaches the end of the string or another '1'. The goal is to maximize the number of such operations.
Approach:
- Identify Positions of '1's: First, we traverse the string to record the positions of all '1's. This helps in understanding the gaps between consecutive '1's and the gaps after the last '1'.
- Calculate Gaps (d_i): For each '1' (except the last one), calculate the number of '0's between it and the next '1'. For the last '1', calculate the number of '0's after it until the end of the string.
-
Compute Operations (m_i): Using the gaps calculated, compute the number of operations that can be performed for each '1'. The recurrence relation used is
m[i] = min(d[i], 1) + m[i+1], starting from the last '1' and moving backwards. This relation ensures that each '1' can be moved at least once if there is a gap, and additional operations are possible if subsequent '1's are moved. -
Sum Operations: The total number of operations is the sum of all computed
m[i]values.
Let's implement this solution in PHP: 3228. Maximum Number of Operations to Move Ones to the End
<?php
/**
* @param String $s
* @return Integer
*/
function maxOperations($s) {
...
...
...
/**
* go to ./solution.php
*/
}
// Test cases
// Example 1
echo maxOperations("1001101") . PHP_EOL; // Output: 4
// Example 2
echo maxOperations("00111") . PHP_EOL; // Output: 0
?>
Explanation:
- Finding Positions: We iterate through the string to collect the indices of all '1's. If there are no '1's, we return 0 since no operations can be performed.
- Calculating Gaps: For each pair of consecutive '1's, the number of '0's between them is calculated by subtracting their indices and adjusting by 1. For the last '1', the number of '0's after it is determined by subtracting its index from the string length and adjusting by 1.
-
Computing Operations: We initialize an array
mto store the number of operations per '1'. Starting from the last '1', we setm[i]to the minimum of the gap and 1. For each preceding '1', we setm[i]to the minimum of its gap and 1 plus the value ofm[i+1]. This ensures that each '1' can be moved at least once if there is a gap, and additional moves are possible if the next '1' is moved, creating new gaps. -
Summing Operations: The total number of operations is the sum of all values in
m, which we return as the result.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:
Top comments (0)