2785. Semi-Ordered Permutation
My accepted Python solution to LeetCode problem 2785, Semi-Ordered Permutation, running in 0ms.
- Difficulty: Easy
- Python
- Runtime 0ms
- Memory 17.5MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 0ms, memory 17.5MB, accepted 2025-12-31.
class Solution:
def semiOrderedPermutation(self, nums: List[int]) -> int:
n = len(nums)
pos1 = nums.index(1)
posn = nums.index(n)
# Moves to bring 1 to front + moves to bring n to back
# If 1 is after n, we save 1 swap
swaps = pos1 + (n - 1 - posn)
if pos1 > posn:
swaps -= 1
return swaps