Js used to prove safety brush offer (in reverse order of the array)

Title Description

Description 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, this array P. finds the total number of reverse pair P and outputs the result modulo 1000000007. I.e., the output P% 1000000007
Input Description:
Title ensure the same number of the input array is not

data range:

对于%50的数据,size<=10^4

对于%75的数据,size<=10^5

对于%100的数据,size<=2*10^5

Cattle off network link

Thinking

Links: https://www.nowcoder.com/questionTerminal/96bd6684e04a44eb80e6a68efc0ec6c5?f=discussion
Source: Cattle-off network

Thinking analysis:
see this title, 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 (the n- 2), so we can consider compare two adjacent numbers.

(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.

Process: first array into sub-arrays, the first count of the number of reverse sub-array inside, then the statistics of the number of reverse between two adjacent sub-arrays. During the reverse of the statistics, we also need to sort the array. If you are familiar with the sorting algorithm, we find that this process is actually a merge sort

js code

function InversePairs(data)
{
    // write code here
    let len = data.length
    if (len === 0) return 0
    const copy = data.concat([])
    let count = InversePairsHelp(data, copy, 0, len-1)
    return count%1000000007
    function InversePairsHelp(data, copy, start, end){
        if (start === end) {
            copy[start] = data[start]
            return 0
        }
        let mid = Math.floor((end-start) / 2)
        let left = InversePairsHelp(copy, data, start, start+mid)
        let right = InversePairsHelp(copy, data, start+mid+1, end)
        let i = start+mid
        let j = end
        let count = 0
        let indexCopy = end
        while(i >= start && j >= start+mid+1){
            if (data[i] > data[j]) {
                copy[indexCopy--] = data[i--] 
                count = count + j - start - mid
            }else {
                copy[indexCopy--] = data[j--]
            }
        }
        for(;i>=start;i--)
           copy[indexCopy--]=data[i]
        for(;j>=start+mid+1;j--)
           copy[indexCopy--]=data[j]      
        return left+right+count
    }
}

Guess you like

Origin www.cnblogs.com/dpnlp/p/yongjs-shua-jian-zhioffer-shu-zu-zhong-de-ni-xu-du.html