493.Reverse-Pairs
493. Reverse Pairs
题目地址
https://leetcode.com/problems/reverse-pairs/
https://lotabout.me/2018/binary-indexed-tree/
题目描述
Given an array nums, we call (i, j) an important reverse pair if i < j and nums[i] > 2*nums[j].
You need to return the number of important reverse pairs in the given array.
Example1:
Input: [1,3,2,3,1]
Output: 2
Example2:
Input: [2,4,3,5,1]
Output: 3
Note:
The length of the given array will not exceed 50,000.
All the numbers in the input array are in the range of 32-bit integer.代码
Approach #1 Merge Sort
Approach #2 BIT
Approach #3 BST
可惜的是,我们自定义的树并不是一个平衡二叉树,在最坏的情况下会导致长链表,如果LeetCode上使用上述代码会导致TLE(Time Limit Exceeded)。
Last updated
Was this helpful?