mah-shamim / leet-code-in-php

Php-based LeetCode algorithm problem solutions, regularly updated.
GNU General Public License v3.0
14 stars 5 forks source link

2938. Separate Black and White Balls #704

Closed mah-shamim closed 8 hours ago

mah-shamim commented 8 hours ago

Discussed in https://github.com/mah-shamim/leet-code-in-php/discussions/703

Originally posted by **mah-shamim** October 16, 2024 **Topics:** `Two Pointers`, `String`, `Greedy` 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:** - 1 <= n == s.length <= 105 - `s[i]` is either `'0'` or `'1'`. **Hint:** 1. Every `1` in the string `s` should be swapped with every `0` on its right side. 2. 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.
mah-shamim commented 8 hours ago

To solve this problem efficiently, we can use a greedy approach with a two-pointer-like strategy. The key insight is that every 1 (black ball) should be moved past the 0s (white balls) that are to its right, minimizing the total number of swaps.

Approach

  1. Track the Number of 0s Encountered:

    • Iterate through the string from right to left.
    • Count the number of 0s encountered so far as you iterate.
    • When you encounter a 1, each 0 that is to its right contributes to a swap needed to move this 1 past those 0s.
    • Add the count of 0s to the total swaps each time you encounter a 1.
  2. Calculate the Total Swaps:

    • The total number of swaps required will be the sum of the number of 0s encountered when processing each 1.

Let's implement this solution in PHP: 2938. Separate Black and White Balls

<?php
/**
 * @param String $s
 * @return Integer
 */
function minSwapsToGroupBlackBalls($s) {
    $n = strlen($s);
    $zeroCount = 0; // Tracks the number of '0's encountered
    $swaps = 0; // Tracks the minimum number of swaps needed

    // Iterate through the string from right to left
    for ($i = $n - 1; $i >= 0; $i--) {
        if ($s[$i] == '0') {
            // Increment the count of zeros when encountering '0'
            $zeroCount++;
        } elseif ($s[$i] == '1') {
            // When encountering '1', add the count of '0's to swaps
            $swaps += $zeroCount;
        }
    }

    return $swaps;
}

// Example usage
$s1 = "101";
echo "Input: $s1\n";
echo "Minimum swaps needed: " . minSwapsToGroupBlackBalls($s1) . "\n"; // Output: 1

$s2 = "100";
echo "Input: $s2\n";
echo "Minimum swaps needed: " . minSwapsToGroupBlackBalls($s2) . "\n"; // Output: 2

$s3 = "0111";
echo "Input: $s3\n";
echo "Minimum swaps needed: " . minSwapsToGroupBlackBalls($s3) . "\n"; // Output: 0
?>

Explanation:

  1. Initialize Counters:

    • zeroCount is initialized to 0 and tracks the number of 0s encountered while iterating from right to left.
    • swaps keeps track of the minimum swaps needed to group the 1s (black balls) together.
  2. Iterate Through the String:

    • Loop through the string from right to left using a for-loop.
    • If the current character is 0, increment zeroCount as it represents a white ball that will need to be swapped with a 1 to its left.
    • If the current character is 1, add zeroCount to swaps because each 0 encountered after this 1 contributes to a swap.
  3. Return the Total Swaps:

    • The accumulated value of swaps represents the minimum number of swaps required to arrange all 1s to the right.

Time Complexity

Example Analysis

This solution provides an efficient way to determine the minimum steps to separate black and white balls using PHP.