Semi-Ordered Permutation — LeetCode 2717 Python Solution
EasyArraySimulation
- Problem
- #2717
- 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
Python
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
| Measure | Complexity |
|---|---|
| Time | O(n), where n is the length of the array |
| Space | O(1) auxiliary |
Related problems
LeetCode 495Teemo AttackingEasyLeetCode 985Sum of Even Numbers After QueriesMediumLeetCode 1389Create Target Array in the Given OrderEasyLeetCode 1409Queries on a Permutation With KeyMediumLeetCode 1503Last Moment Before All Ants Fall Out of a PlankMediumLeetCode 1535Find the Winner of an Array GameMedium
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.