493. Reverse Pairs
Read the full problem statement on LeetCode.
Difficulty: hard Acceptance: 32% Topics: Array, Binary Search, Divide and Conquer, Binary Indexed Tree, Segment Tree, Merge Sort, Ordered Set
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# 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)
Solution from kamyu104/LeetCode-Solutions · MIT
Similar questions