Skip to content

Commit eea98c7

Browse files
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 31.9 MB (0.00%)
1 parent 567bac5 commit eea98c7

2 files changed

Lines changed: 82 additions & 0 deletions

File tree

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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>&nbsp;</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>&nbsp;</p>
58+
<p><strong>Constraints:</strong></p>
59+
60+
<ul>
61+
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
62+
<li><code>0 &lt;= nums[i] &lt;= 100</code></li>
63+
</ul>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
int minimumSwaps(vector<int>& nums) {
4+
int n = nums.size();
5+
int ans = 0;
6+
int zeros = count(nums.begin(), nums.end(), 0);
7+
for (int i=n-1; i>=0; i--) {
8+
if (zeros == 0) break;
9+
int val = nums[i];
10+
if (val == 0) {
11+
zeros--;
12+
continue;
13+
}
14+
ans++;
15+
zeros--;
16+
}
17+
return ans;
18+
}
19+
};

0 commit comments

Comments
 (0)