-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path数组中的逆序对.py
71 lines (64 loc) · 2.43 KB
/
数组中的逆序对.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
'''
在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。
输入一个数组,求出这个数组中的逆序对的总数P。
'''
# -*- coding:utf-8 -*-
class Solution:
def InversePairs(self, data):
length = len(data)
if data == None or length <= 0:
return 0
copy = [0]*length
for i in range(length):
copy[i] = data[i]
count = self.InversePairsCore(data, copy, 0, length-1)
return count
def InversePairsCore(self, data, copy, start, end):
if start == end:
copy[start] = data[start]
return 0
length = (end - start)//2
left = self.InversePairsCore(copy, data, start, start+length)
right = self.InversePairsCore(copy, data, start+length+1, end)
# i初始化为前半段最后一个数字的下标
i = start + length
# j初始化为后半段最后一个数字的下标
j = end
indexCopy = end
count = 0
while i >= start and j >= start+length+1:
if data[i] > data[j]:
copy[indexCopy] = data[i]
indexCopy -= 1
i -= 1
count += j - start - length
else:
copy[indexCopy] = data[j]
indexCopy -= 1
j -= 1
while i >= start:
copy[indexCopy] = data[i]
indexCopy -= 1
i -= 1
while j >= start+length+1:
copy[indexCopy] = data[j]
indexCopy -= 1
j -= 1
return left + right + count
# 使用数据的index进行求解
def InversePairs2(self, data):
if len(data) <= 0:
return 0
count = 0
copy = []
for i in range(len(data)):
copy.append(data[i])
copy.sort()
i = 0
while len(copy) > i:
count += data.index(copy[i])
data.remove(copy[i])
i += 1
return count
s = Solution()
print(s.InversePairs2([364,637,341,406,747,995,234,971,571,219,993,407,416,366,315,301,601,650,418,355,460,505,360,965,516,648,727,667,465,849,455,181,486,149,588,233,144,174,557,67,746,550,474,162,268,142,463,221,882,576,604,739,288,569,256,936,275,401,497,82,935,983,583,523,697,478,147,795,380,973,958,115,773,870,259,655,446,863,735,784,3,671,433,630,425,930,64,266,235,187,284,665,874,80,45,848,38,811,267,575]))