Class Solution
java.lang.Object
g2701_2800.s2717_semi_ordered_permutation.Solution
2717 - Semi-Ordered Permutation.<p>Easy</p>
<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><strong>Example 1:</strong></p>
<p><strong>Input:</strong> nums = [2,1,4,3]</p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong> We can make the permutation semi-ordered using these sequence of operations:</p>
<p>1 - swap i = 0 and j = 1. The permutation becomes [1,2,4,3].</p>
<p>2 - swap i = 2 and j = 3. The permutation becomes [1,2,3,4].</p>
<p>It can be proved that there is no sequence of less than two operations that make nums a semi-ordered permutation.</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> nums = [2,4,1,3]</p>
<p><strong>Output:</strong> 3</p>
<p><strong>Explanation:</strong> We can make the permutation semi-ordered using these sequence of operations:</p>
<p>1 - swap i = 1 and j = 2. The permutation becomes [2,1,4,3].</p>
<p>2 - swap i = 0 and j = 1. The permutation becomes [1,2,4,3].</p>
<p>3 - swap i = 2 and j = 3. The permutation becomes [1,2,3,4].</p>
<p>It can be proved that there is no sequence of less than three operations that make nums a semi-ordered permutation.</p>
<p><strong>Example 3:</strong></p>
<p><strong>Input:</strong> nums = [1,3,4,2,5]</p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong> The permutation is already a semi-ordered permutation.</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>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
semiOrderedPermutation
public int semiOrderedPermutation(int[] nums)
-