Old Wei wins the offer to take you to learn --- Brush title series (in reverse order of 35. The array)

35. The array of reverse

problem:

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, this array P. finds the total number of reverse pair P and outputs the result modulo 1000000007. I.e., the output P% 1000000007

solve:

thought:

See this topic, our first reaction is a sequential scan of the entire array. Each time an array to scan, one by comparing the number and size of the digital behind it. If the latter figure is smaller than it is, then the two figures on the formation of a reverse pair. Suppose the array contains n digits. Since each digital and must be O (n) comparing this number, the time complexity of the algorithm is O (n ^ 2).
We have an array {7,5,6,4} as an example to the statistical analysis of the reverse. Each scan to a digital, we do not get ta and every digital back for comparison, otherwise the time complexity is O (n ^ 2), so we can consider compare two adjacent numbers.
Here Insert Picture Description
(a) the length of the array 4 into two sub-arrays of length 2;
(B) the sub-array of an array of length 2 into two Chengdu 1;
(C) the length of the sub-array 1 were combined, sorting and counting of reverse;
(D) the length of the subarray 2 combined sorting, and counting on the reverse;
in FIG. (a) and (b), we first decomposed into two arrays of length 2 sub-array, then the two sub-arrays are split into two sub-arrays of length 1. Next, while merging adjacent sub-arrays, while statistics on the number of reverse. In the first sub-array length 7 {1}, {5} is greater than 7, 5, so (7,5) to form a reverse pair. Also in the second sub-array length 1 {6}, {4} is also a reverse order (6,4). Since we have two pairs of internal statistics array reverse order, it is necessary to sort the two sub array shown above (c), in order to avoid repeated in subsequent statistical statistical process.
Next, we reverse the statistical length between two sub-sub-array 2 array. The combined process of the subarray and the following statistical reverse as shown in FIG.
We were first with two pointers to the end of the two sub-arrays, and compare the two figures each pointer. If the number of the first sub-array number greater than the second array, the reverse configuration of the number equal to the number of reverse and a second sub-array in the remaining digits, as shown below (a) and (c), shows. If the first number is less than or equal to the array in the second array does not constitute a reverse order, as shown in FIG b. Every time comparisons, we regard the higher number copied from the back forward to an auxiliary array, make sure that the auxiliary array (referred to as copy) The number in ascending order of. After the large digital copying to the secondary array, corresponding to the forward movement of a pointer, then the next round of comparison.
Here Insert Picture Description

python code:

# -*- coding:utf-8 -*-
import copy
class Solution:
    def InversePairs(self, data):
        return self.sort(data[:], 0, len(data)-1, data[:]) % 1000000007

    def sort(self, temp, left, right, data):
        ### 先考虑每个分组只有一个元素的时候
        if right - left < 1:
            return 0
        if right - left == 1:
            if data[left] < data[right]:
                return 0
            else:
                temp[left], temp[right] = data[right], data[left]
                return 1
        #### 分组
        mid = (left + right) // 2
        res = self.sort(data, left, mid, temp) + self.sort(data, mid+1, right, temp)
		# 合并分组
        i = left
        j = mid + 1
        index = left

        while i <= mid and j <= right:
            if data[i] <= data[j]:
                temp[index] = data[i]
                i += 1
            else:
                temp[index] = data[j]
                res += mid +1-i
                j += 1
            index += 1
        while i <= mid:
            temp[index] = data[i]
            i += 1
            index += 1
        while j <= right:
            temp[index] = data[j]
            j += 1
            index += 1
        return res



Published 160 original articles · won praise 30 · views 70000 +

Guess you like

Origin blog.csdn.net/yixieling4397/article/details/105022755