2340. Minimum Adjacent Swaps to Make a Valid Array

Обновлено: 2024-03-12
1 мин
[]

Содержание

LeetCode problem 2340

class Solution:
    def minimumSwaps(self, nums: List[int]) -> int:
        i = j = 0
        for k, v in enumerate(nums):
            if v < nums[i] or (v == nums[i] and k < i):
                i = k
            if v >= nums[j] or (v == nums[j] and k > j):
                j = k
        return 0 if i == j else i + len(nums) - 1 - j - (i > j)