-
Notifications
You must be signed in to change notification settings - Fork 4
/
ShuZuZhongDeNiXuDuiLcof.py
41 lines (33 loc) · 1.08 KB
/
ShuZuZhongDeNiXuDuiLcof.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
from typing import List
def reversePairs(nums: List[int]) -> int:
res = 0
if not nums:
return 0
def merge(left, right):
nonlocal res
if left == right:
return [nums[left]]
mid = (left + right) // 2
left_nums = merge(left, mid)
right_nums = merge(mid + 1, right)
point1, point2, merge_num, len1, len2 = 0, 0, [], len(left_nums), len(right_nums)
while point1 < len1 and point2 < len2:
if left_nums[point1] <= right_nums[point2]:
merge_num.append(left_nums[point1])
point1 += 1
res += point2
else:
merge_num.append(right_nums[point2])
point2 += 1
while point1 < len1:
res += len2
merge_num.append(left_nums[point1])
point1 += 1
while point2 < len2:
merge_num.append(right_nums[point2])
point2 += 1
return merge_num
merge(0, len(nums) - 1)
return res
if __name__ == '__main__':
print(reversePairs([7,5,6,4,5,6,4]))