|
| 1 | +<p>You are given an integer array <code>nums</code>.</p> |
| 2 | + |
| 3 | +<p>In one operation, you can choose any two <strong>distinct</strong> indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code>.</p> |
| 4 | + |
| 5 | +<p>Return an integer denoting the <strong>minimum</strong> number of operations required to move all 0s to the end of the array.</p> |
| 6 | + |
| 7 | +<p> </p> |
| 8 | +<p><strong class="example">Example 1:</strong></p> |
| 9 | + |
| 10 | +<div class="example-block"> |
| 11 | +<p><strong>Input:</strong> <span class="example-io">nums = [0,1,0,3,12]</span></p> |
| 12 | + |
| 13 | +<p><strong>Output:</strong> <span class="example-io">2</span></p> |
| 14 | + |
| 15 | +<p><strong>Explanation:</strong></p> |
| 16 | + |
| 17 | +<p>We perform the following swap operations:</p> |
| 18 | + |
| 19 | +<ul> |
| 20 | + <li>Swap <code>nums[0]</code> and <code>nums[3]</code>, giving <code>nums = [3, 1, 0, 0, 12]</code>.</li> |
| 21 | + <li>Swap <code>nums[2]</code> and <code>nums[4]</code>, giving <code>nums = [3, 1, 12, 0, 0]</code>.</li> |
| 22 | +</ul> |
| 23 | + |
| 24 | +<p>Thus, the answer is 2.</p> |
| 25 | +</div> |
| 26 | + |
| 27 | +<p><strong class="example">Example 2:</strong></p> |
| 28 | + |
| 29 | +<div class="example-block"> |
| 30 | +<p><strong>Input:</strong> <span class="example-io">nums = [0,1,0,2]</span></p> |
| 31 | + |
| 32 | +<p><strong>Output:</strong> <span class="example-io">1</span></p> |
| 33 | + |
| 34 | +<p><strong>Explanation:</strong></p> |
| 35 | + |
| 36 | +<p>We perform the following swap operations:</p> |
| 37 | + |
| 38 | +<ul> |
| 39 | + <li>Swap <code>nums[0]</code> and <code>nums[3]</code>, giving <code>nums = [2, 1, 0, 0]</code>.</li> |
| 40 | +</ul> |
| 41 | + |
| 42 | +<p>Thus, the answer is 1.</p> |
| 43 | +</div> |
| 44 | + |
| 45 | +<p><strong class="example">Example 3:</strong></p> |
| 46 | + |
| 47 | +<div class="example-block"> |
| 48 | +<p><strong>Input:</strong> <span class="example-io">nums = [1,2,0]</span></p> |
| 49 | + |
| 50 | +<p><strong>Output:</strong> <span class="example-io">0</span></p> |
| 51 | + |
| 52 | +<p><strong>Explanation:</strong></p> |
| 53 | + |
| 54 | +<p>The array already satisfies the condition. Therefore, no swap operations are needed.</p> |
| 55 | +</div> |
| 56 | + |
| 57 | +<p> </p> |
| 58 | +<p><strong>Constraints:</strong></p> |
| 59 | + |
| 60 | +<ul> |
| 61 | + <li><code>1 <= nums.length <= 100</code></li> |
| 62 | + <li><code>0 <= nums[i] <= 100</code></li> |
| 63 | +</ul> |
0 commit comments