mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-11 02:58:13 +08:00
45 lines
1.7 KiB
HTML
45 lines
1.7 KiB
HTML
|
<p>There are <code>n</code> balls on a table, each ball has a color black or white.</p>
|
||
|
|
||
|
<p>You are given a <strong>0-indexed</strong> binary string <code>s</code> of length <code>n</code>, where <code>1</code> and <code>0</code> represent black and white balls, respectively.</p>
|
||
|
|
||
|
<p>In each step, you can choose two adjacent balls and swap them.</p>
|
||
|
|
||
|
<p>Return <em>the <strong>minimum</strong> number of steps to group all the black balls to the right and all the white balls to the left</em>.</p>
|
||
|
|
||
|
<p> </p>
|
||
|
<p><strong class="example">Example 1:</strong></p>
|
||
|
|
||
|
<pre>
|
||
|
<strong>Input:</strong> s = "101"
|
||
|
<strong>Output:</strong> 1
|
||
|
<strong>Explanation:</strong> 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.</pre>
|
||
|
|
||
|
<p><strong class="example">Example 2:</strong></p>
|
||
|
|
||
|
<pre>
|
||
|
<strong>Input:</strong> s = "100"
|
||
|
<strong>Output:</strong> 2
|
||
|
<strong>Explanation:</strong> 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.
|
||
|
</pre>
|
||
|
|
||
|
<p><strong class="example">Example 3:</strong></p>
|
||
|
|
||
|
<pre>
|
||
|
<strong>Input:</strong> s = "0111"
|
||
|
<strong>Output:</strong> 0
|
||
|
<strong>Explanation:</strong> All the black balls are already grouped to the right.
|
||
|
</pre>
|
||
|
|
||
|
<p> </p>
|
||
|
<p><strong>Constraints:</strong></p>
|
||
|
|
||
|
<ul>
|
||
|
<li><code>1 <= n == s.length <= 10<sup>5</sup></code></li>
|
||
|
<li><code>s[i]</code> is either <code>'0'</code> or <code>'1'</code>.</li>
|
||
|
</ul>
|