Участник:ArtemTovkes/minimum-swaps-to-make-sequences-increasing

Материал из DISCOPAL
Перейти к: навигация, поиск

Python3

https://leetcode.com/problems/minimum-swaps-to-make-sequences-increasing/submissions/

class Solution:
    def minSwap(self, A: List[int], B: List[int]) -> int:
        swap = 1
        stay = 0
 
        for i in range(1, len(A)):
            if A[i] <= B[i-1] or B[i] <= A[i-1]:
                swap = swap+1
            elif A[i] <= A[i-1] or B[i] <= B[i-1]:
                swap, stay = stay+1, swap
            else:
                minval = min(swap, stay)
                stay = minval
                swap = minval + 1
 
        return min(swap, stay)