|
| 1 | +<p>Given a binary array <code>data</code>, return the minimum number of swaps required to group all <code>1</code>’s present in the array together in <strong>any place</strong> in the array.</p> |
| 2 | + |
| 3 | +<p> </p> |
| 4 | +<p><strong class="example">Example 1:</strong></p> |
| 5 | + |
| 6 | +<pre> |
| 7 | +<strong>Input:</strong> data = [1,0,1,0,1] |
| 8 | +<strong>Output:</strong> 1 |
| 9 | +<strong>Explanation:</strong> There are 3 ways to group all 1's together: |
| 10 | +[1,1,1,0,0] using 1 swap. |
| 11 | +[0,1,1,1,0] using 2 swaps. |
| 12 | +[0,0,1,1,1] using 1 swap. |
| 13 | +The minimum is 1. |
| 14 | +</pre> |
| 15 | + |
| 16 | +<p><strong class="example">Example 2:</strong></p> |
| 17 | + |
| 18 | +<pre> |
| 19 | +<strong>Input:</strong> data = [0,0,0,1,0] |
| 20 | +<strong>Output:</strong> 0 |
| 21 | +<strong>Explanation:</strong> Since there is only one 1 in the array, no swaps are needed. |
| 22 | +</pre> |
| 23 | + |
| 24 | +<p><strong class="example">Example 3:</strong></p> |
| 25 | + |
| 26 | +<pre> |
| 27 | +<strong>Input:</strong> data = [1,0,1,0,1,0,0,1,1,0,1] |
| 28 | +<strong>Output:</strong> 3 |
| 29 | +<strong>Explanation:</strong> One possible solution that uses 3 swaps is [0,0,0,0,0,1,1,1,1,1,1]. |
| 30 | +</pre> |
| 31 | + |
| 32 | +<p> </p> |
| 33 | +<p><strong>Constraints:</strong></p> |
| 34 | + |
| 35 | +<ul> |
| 36 | + <li><code>1 <= data.length <= 10<sup>5</sup></code></li> |
| 37 | + <li><code>data[i]</code> is either <code>0</code> or <code>1</code>.</li> |
| 38 | +</ul> |
0 commit comments