EasyNo. 2717
Semi-Ordered Permutation — LeetCode 2717 Python Solution
- Topics
- Array, Simulation
- Reading time
- 2 min
- Source
- leetcode.com
The problem
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.
Example
- Input
- nums = [2,1,4,3]
- Output
- 2
- Explanation
- We can make the permutation semi-ordered using these sequence of operations:
Python solution
class Solution:
def semiOrderedPermutation(self, nums: List[int]) -> int:
n = len(nums)
i = nums.index(1)
j = nums.index(n)
k = 1 if i < j else 2
return i + n - j - kComplexity
- Time
- O(n)where n is the length of the array
- Space · auxiliary
- O(1)
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 0495 | Teemo Attacking | Easy |
| 0985 | Sum of Even Numbers After Queries | Medium |
| 1389 | Create Target Array in the Given Order | Easy |
| 1409 | Queries on a Permutation With Key | Medium |
| 1503 | Last Moment Before All Ants Fall Out of a Plank | Medium |
| 1535 | Find the Winner of an Array Game | Medium |
Frequently asked questions
- How hard is LeetCode 2717. Semi-Ordered Permutation?
LeetCode 2717. Semi-Ordered Permutation is rated Easy on LeetCode.
- What is the time complexity of LeetCode 2717. Semi-Ordered Permutation?
The Python solution on this page runs in O(n), where n is the length of the array.
- What is the space complexity of LeetCode 2717. Semi-Ordered Permutation?
The Python solution on this page uses O(1) auxiliary space.
- What topics does LeetCode 2717. Semi-Ordered Permutation cover?
LeetCode 2717. Semi-Ordered Permutation is tagged Array and Simulation on LeetCode.