51 of the array in reverse order

Title: two numbers in the array, if a number is greater than the front behind the figures, the two numbers form a reverse pair. Enter an array, the array is determined in reverse right.

def inverse_pairs(arrys):
    if len(arrys)<2:
        return 0

    res = 0
    length = len(arrys)
    arrys_sort =sorted(arrys)
    for arr in arrys_sort:
        res += arrys.index(arr)
        arrys.remove(arr)

    return res

  NOTE: official to using merge sort method, time complexity is O (nlgn). In this paper, their own methods, time complexity is O (n2), needs to be improved, but the implementation is very simple. First Sorting an array, use the built-in sort function. Then, traversing the sorted array, the number and the index in order to find the original array, the index difference is the number of reverse and the number of components.

Guess you like

Origin www.cnblogs.com/kingshine007/p/11502588.html