The Indian Engineer

Problem 2938 Separate Black and White Balls

Posted on 6 mins

Two-Pointers String Greedy

Problem Statement

Link - Problem 2938

Question

There are n balls on a table, each ball has a color black or white.

You are given a 0-indexed binary string s of length n, where 1 and 0 represent black and white balls, respectively.

In each step, you can choose two adjacent balls and swap them.

Return the minimum number of steps to group all the black balls to the right and all the white balls to the left.

Example 1:

Input: s = "101"
Output: 1
Explanation: We can group all the black balls to the right in the following way:
- Swap s[0] and s[1], s = "011".
Initially, 1s are not grouped together, requiring at least 1 step to group them to the right.

Example 2:

Input: s = "100"
Output: 2
Explanation: We can group all the black balls to the right in the following way:
- Swap s[0] and s[1], s = "010".
- Swap s[1] and s[2], s = "001".
It can be proven that the minimum number of steps needed is 2.

Example 3:

Input: s = "0111"
Output: 0
Explanation: All the black balls are already grouped to the right.

Constraints:

Solution

class Solution {
public:
    long long minimumSteps(string s) {
        int n = s.length();
        long long ans = 0;
        int black_ball_cnt = 0;

        for (int i = 0; i < n; i++) {
            if (s[i] == '0') {
                // White ball encountered, add the number of black balls on its left
                ans += black_ball_cnt;
            } else {
                // Black ball encountered, increment the black ball count
                black_ball_cnt++;
            }
        }
        return ans;
    }
};

Complexity Analysis

| Algorithm             | Time Complexity | Space Complexity |
| --------------------- | --------------- | ---------------- |
| Single-pass traversal | O(n)            | O(1)             |

Explanation

Intial Thoughts

To approach this problem, consider the task as sorting the balls by color. Think of the string as a sequence of balls, and the goal is to group all the black balls to the right and all the white balls to the left. Another approach could be to count the number of inversions, where an inversion is a pair of elements in the wrong order. Breaking down the problem into smaller steps can also help, such as focusing on one ball at a time and determining the minimum number of swaps needed to move it to its correct position. Additionally, visualizing the process and using real-life examples can aid in understanding. Finally, considering the constraints and the properties of the string can also provide insights.

Intuitive Analysis

Analyze the problem by thinking about the minimum number of swaps required to move all the black balls to the right. It can be helpful to think of it as the number of times a black ball and a white ball are in the wrong positions, which can be calculated by counting the number of black balls to the left of each white ball. Intuitively, the solution involves counting the number of such pairs and returning this count as the minimum number of steps. Furthermore, understanding the concept of adjacency and how swapping adjacent balls affects the overall arrangement can also lead to an intuitive solution. Moreover, recognizing that the optimal solution involves moving the black balls as few times as possible can guide the development of an efficient algorithm. Lastly, thinking about the problem in terms of ‘inversions’ can also lead to an intuitive understanding of the solution.

1. Intuition

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:


Footnote

This question is rated as Medium difficulty.

Hints

Every 1 in the string s should be swapped with every 0 on its right side.

Iterate right to left and count the number of 0 that have already occurred, whenever you iterate on 1 add that counter to the answer.