剑指offer35.数组中的逆序对

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_36811967/article/details/86511385

题目描述
在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数P。并将P对1000000007取模的结果输出。 即输出P%1000000007

输入描述:
题目保证输入的数组中没有的相同的数字
数据范围:
对于%50的数据,size<=10^4
对于%75的数据,size<=10^5
对于%100的数据,size<=2*10^5
示例1
输入
复制
1,2,3,4,5,6,7,0
输出
复制
7

首先就想到了暴力法,肯定是超时了:

# -*- coding:utf-8 -*-
class Solution:
    def InversePairs(self, data):
        # write code here
        if len(data) <= 1:
            return 0
        count = 0
        for i in range(len(data)-1):
            for j in range(i+1, len(data)):
                if data[i] > data[j]:
                    count += 1
        return count%1000000007

还可以先将原序列排序,然后从排完序的数组中取出最小的,它在原数组中的位置表示有多少比它大的数在它前面,每取出一个在原数组中删除该元素,保证后面取出的元素在原数组中是最小的,这样其位置才能表示有多少比它大的数在它前面,即逆序对数。 这样还是超时~~~

# -*- coding:utf-8 -*-
class Solution:
    def InversePairs(self, data):
        # write code here
        copy, count = [], 0
        for num in data:
            copy.append(num)
        copy.sort()
        for num in copy:
            count += data.index(num)  # 最小值index位置表示前面有index个原始大于它
            data.remove(num)  # 移除这个num
        return count%1000000007

下面基于归并,能通过75%:

# -*- coding:utf-8 -*-
class Solution:
    def InversePairs(self, data):
        # write code here
        global count
        count = 0
        def merge_sort(data):
            global count
            if len(data) <= 1:
                return data
            mid, array = len(data)//2, []
            left_a, right_a = merge_sort(data[:mid]), merge_sort(data[mid:])
            left, right = 0, 0
            while left < len(left_a) and right < len(right_a):  # 左右已经有序
                if left_a[left] <= right_a[right]:
                    array.append(left_a[left])
                    left += 1
                else:
                    array.append(right_a[right])
                    count += len(left_a)-left  # 若右边小则要加上所有左边剩下的
                    right += 1
            array += left_a[left:]
            array += right_a[right:]
            return array
        res = merge_sort(data)
        return count%1000000007

Python就到不了2s之内呀!佛了~~~:

# -*- coding:utf-8 -*-
class Solution:
    def InversePairs(self, data):
        # write code here
        global count
        count = 0
        def merge_sort(data):
            global count
            if len(data) <= 1:
                return data
            mid = len(data)//2
            left_a, right_a = merge_sort(data[:mid]), merge_sort(data[mid:])
            left, right, index = 0, 0, 0
            while left < len(left_a) and right < len(right_a):  # 左右已经有序
                if left_a[left] <= right_a[right]:
                    data[index] = left_a[left]
                    left += 1
                else:
                    data[index] = right_a[right]
                    count += len(left_a)-left  # 若右边小则要加上所有左边剩下的
                    right += 1
                index += 1
            while left < len(left_a):
                data[index] = left_a[left]
                left += 1
                index += 1
            while right < len(right_a):
                data[index] = right_a[right]
                right += 1
                index += 1
            return data
        res = merge_sort(data)
        return count%1000000007

猜你喜欢

转载自blog.csdn.net/sinat_36811967/article/details/86511385