forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverse-pairs.py
35 lines (30 loc) · 1.04 KB
/
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
# Time: O(nlogn)
# Space: O(n)
class Solution(object):
def reversePairs(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
def merge(nums, start, mid, end):
r = mid + 1
tmp = []
for i in xrange(start, mid + 1):
while r <= end and nums[i] > nums[r]:
tmp.append(nums[r])
r += 1
tmp.append(nums[i])
nums[start:start+len(tmp)] = tmp
def countAndMergeSort(nums, start, end):
if end - start <= 0:
return 0
mid = start + (end - start) / 2
count = countAndMergeSort(nums, start, mid) + countAndMergeSort(nums, mid + 1, end)
r = mid + 1
for i in xrange(start, mid + 1):
while r <= end and nums[i] > nums[r] * 2:
r += 1
count += r - (mid + 1)
merge(nums, start, mid, end)
return count
return countAndMergeSort(nums, 0, len(nums) - 1)