Team Ai
Datasetpublic

FPEvalRepoPublic/LeetCodeMetaData

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes399downloads
semi-ordered-permutation.json36 linesDownload Raw Back to root
1{2  "id": 2785,3  "name": "semi-ordered-permutation",4  "difficulty": "Easy",5  "link": "https://leetcode.com/problems/semi-ordered-permutation/",6  "date": "2023-05-28",7  "task_description": "You are given a **0-indexed** permutation of `n` integers `nums`. A permutation is called **semi-ordered** if the first number equals `1` and the last number equals `n`. You can perform the below operation as many times as you want until you make `nums` a **semi-ordered** permutation: Pick two adjacent elements in `nums`, then swap them. Return _the minimum number of operations to make _`nums`_ a **semi-ordered permutation**_. A **permutation** is a sequence of integers from `1` to `n` of length `n` containing each number exactly once. **Example 1:** ``` **Input:** nums = [2,1,4,3] **Output:** 2 **Explanation:** 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. ``` **Example 2:** ``` **Input:** nums = [2,4,1,3] **Output:** 3 **Explanation:** 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. ``` **Example 3:** ``` **Input:** nums = [1,3,4,2,5] **Output:** 0 **Explanation:** The permutation is already a semi-ordered permutation. ``` **Constraints:** `2 <= nums.length == n <= 50` `1 <= nums[i] <= 50` `nums is a permutation.`",8  "test_case": [9    {10      "label": "Example 1",11      "input": "nums = [2,1,4,3]",12      "output": "2 "13    },14    {15      "label": "Example 2",16      "input": "nums = [2,4,1,3]",17      "output": "3 "18    },19    {20      "label": "Example 3",21      "input": "nums = [1,3,4,2,5]",22      "output": "0 "23    }24  ],25  "constraints": [26    "Pick two adjacent elements in nums, then swap them.",27    "2 <= nums.length == n <= 50",28    "1 <= nums[i] <= 50",29    "nums is a permutation."30  ],31  "python_template": "class Solution(object):\n    def semiOrderedPermutation(self, nums):\n        \"\"\"\n        :type nums: List[int]\n        :rtype: int\n        \"\"\"\n        ",32  "java_template": "class Solution {\n    public int semiOrderedPermutation(int[] nums) {\n        \n    }\n}",33  "metadata": {34    "func_name": "semiOrderedPermutation"35  }36}