---
# System prepended metadata

title: 2340. Minimum Adjacent Swaps to Make a Valid Array
tags: [Leetcode, Medium, Greedy]

---

Link: https://leetcode.com/problems/minimum-adjacent-swaps-to-make-a-valid-array/description/
## 思路
To get the minium number of swaps, we need to:

find the index of the rightmost largest number ```nums[maxi]```
find the index of the leftmost smallest number ```nums[mini]```

if ```maxi>mini``` we subtract the swap number by 1
## Code
```python=
class Solution:
    def minimumSwaps(self, nums: List[int]) -> int:
        maxi = len(nums)-1
        mini = 0
        for i, num in enumerate(nums):
            if nums[i]<nums[mini]:
                mini = i
            if nums[i]>=nums[maxi]:
                maxi = i
        return len(nums)-1-maxi+mini-(1 if mini>maxi else 0)
```