global count count = 0 defInversePairs(data): defcore(data): # write code here if len(data) <= 1: return data num = int(len(data) / 2)
left = core(data[:num]) right =core(data[num:]) return Merge(left, right) core(data) return count
#合并各个子数组 defMerge(left, right): global count l1 = len(left)-1 l2 = len(right)-1 res = [] num = 0 while l1>=0and l2>=0: if left[l1]<=right[l2]: res = [right[l2]]+res l2-=1 else: res = [left[l1]]+res count += l2+1 l1-=1 while l1>=0: res = [left[l1]]+res l1-=1 while l2>=0: res = [right[l2]]+res l2-=1 return res