mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-05 23:41:41 +08:00
update
This commit is contained in:
52
leetcode/problem/semi-ordered-permutation.html
Normal file
52
leetcode/problem/semi-ordered-permutation.html
Normal file
@@ -0,0 +1,52 @@
|
||||
<p>You are given a <strong>0-indexed</strong> permutation of <code>n</code> integers <code>nums</code>.</p>
|
||||
|
||||
<p>A permutation is called <strong>semi-ordered</strong> if the first number equals <code>1</code> and the last number equals <code>n</code>. You can perform the below operation as many times as you want until you make <code>nums</code> a <strong>semi-ordered</strong> permutation:</p>
|
||||
|
||||
<ul>
|
||||
<li>Pick two adjacent elements in <code>nums</code>, then swap them.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the minimum number of operations to make </em><code>nums</code><em> a <strong>semi-ordered permutation</strong></em>.</p>
|
||||
|
||||
<p>A <strong>permutation</strong> is a sequence of integers from <code>1</code> to <code>n</code> of length <code>n</code> containing each number exactly once.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,1,4,3]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We can make the permutation semi-ordered using these sequence of operations:
|
||||
1 - swap i = 0 and j = 1. The permutation becomes [1,2,4,3].
|
||||
2 - swap i = 2 and j = 3. The permutation becomes [1,2,3,4].
|
||||
It can be proved that there is no sequence of less than two operations that make nums a semi-ordered permutation.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,4,1,3]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> We can make the permutation semi-ordered using these sequence of operations:
|
||||
1 - swap i = 1 and j = 2. The permutation becomes [2,1,4,3].
|
||||
2 - swap i = 0 and j = 1. The permutation becomes [1,2,4,3].
|
||||
3 - swap i = 2 and j = 3. The permutation becomes [1,2,3,4].
|
||||
It can be proved that there is no sequence of less than three operations that make nums a semi-ordered permutation.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,3,4,2,5]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> The permutation is already a semi-ordered permutation.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length == n <= 50</code></li>
|
||||
<li><code>1 <= nums[i] <= 50</code></li>
|
||||
<li><code>nums is a permutation.</code></li>
|
||||
</ul>
|
Reference in New Issue
Block a user