Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to find reverse pairs in arrays by LeetCode

Shulou Source: shulou.com Published: 2022-06-01 13:05:12 09月22日 Update

This article mainly introduces LeetCode how to find the reverse pairs in the array, the article is very detailed, has a certain reference value, interested friends must read it!

Topic description

Two numbers in an array that form a reverse pair if the first number is greater than the next. Enter an array to find the total number of reverse pairs in this array.

0 = e:

Return

M = (s + e) > > 1

# sort and count the left and right parts first

Mergesort (s, m)

Mergesort (m + 1, e)

# use temporary arrays to store sorted left and right parts

# Note that reverse storage is selected here because the smallest element can be obtained by using the pop operation of O (1).

# of course, double-ended queues can also be used here, so that there is no need to reverse order

Left = nums [svm + 1] [::-1]

Right = nums [m + 1 e + 1] [::-1]

For i in range (s, e + 1):

If not right or left and left [- 1]

Tags: Reverse order array number part sort complexity right train of thought time result topic find order two number code content that is number Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Huawei MariaDB Docker macOS