-
Notifications
You must be signed in to change notification settings - Fork 14
/
493-reverse-pairs.py
48 lines (42 loc) · 1.48 KB
/
493-reverse-pairs.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
class Solution:
def reversePairs(self, nums: List[int]) -> int:
count = 0
def merge_sort(nums):
nonlocal count
n = len(nums)
if n <= 1:
return nums
m = n // 2
left_part = merge_sort(nums[: m])
right_part = merge_sort(nums[m:])
left, right = 0, 0
while left < m and right < n - m:
if left_part[left] > right_part[right] * 2:
count += m - left
right += 1
else:
left += 1
res = []
left, right = 0, 0
while left < m or right < n - m:
if left == m:
res.append(right_part[right])
right += 1
elif right == n - m:
res.append(left_part[left])
left += 1
elif left_part[left] <= right_part[right]:
res.append(left_part[left])
left += 1
else:
res.append(right_part[right])
right += 1
return res
merge_sort(nums)
return count
# time O(nlogn), each layer to merge cost O(n)
# space O(n), due to new list, merge sort has logn stack layers
# using array and sort and merge sort and two pointers
'''
1. the count is updated if left part element is twice larger than right part element
'''