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:

  1. The length of the given array will not exceed 50,000.
  2. All the numbers in the input array are in the range of 32-bit integer.
 

Approach #1: Brute Force.

class Solution {
public:
int reversePairs(vector<int>& nums) {
int len = nums.size();
int count = 0;
for (int i = 0; i < len; ++i) {
for (int j = i + 1; j < len; ++j) {
if (nums[i] > nums[j] * 2LL) count++;
}
}
return count;
}
};

  

Approach #2: Binary Search Tree.

class Node {
public:
int val, count_ge;
Node *left, *right;
Node(int val) {
this->val = val;
this->count_ge = 1;
this->left = NULL;
this->right = NULL;
}
}; class Solution {
public:
int reversePairs(vector<int>& nums) {
int len = nums.size();
int count = 0;
Node* head = NULL; for (int i = 0; i < len; ++i) {
count += search(head, nums[i] * 2LL + 1);
head = insert(head, nums[i]);
} return count;
} private:
int search(Node* head, long long val) {
if (head == NULL)
return 0;
else if (head->val == val) {
return head->count_ge;
} else if (head->val > val) {
return head->count_ge + search(head->left, val);
} else {
return search(head->right, val);
}
} Node* insert(Node* head, int val) {
if (head == NULL) return new Node(val);
else if (head->val == val)
head->count_ge++;
else if (head->val < val) {
head->count_ge++;
head->right = insert(head->right, val);
} else {
head->left = insert(head->left, val);
}
return head;
}
};

  

Approach #3: Binary Index Tree.

class Solution {
public int reversePairs(int[] nums) {
if (nums == null || nums.length <= 1) return 0;
int n = nums.length;
int[] nums_copy = nums.clone(); Arrays.sort(nums_copy); int[] BITS = new int[n+1]; int count = 0; for (int i = n-1; i >= 0; --i) {
count += query(BITS, index(nums_copy, 1.0 * nums[i] / 2));
update(BITS, index(nums_copy, nums[i]));
} return count;
} private void update(int[] BIT, int index) {
index = index + 1;
while (index < BIT.length) {
BIT[index]++;
index += index & (-index);
}
} private int query(int[] BIT, int index) {
int sum = 0;
while (index > 0) {
sum += BIT[index];
index -= index & (-index);
}
return sum;
} private int index(int[] arr, double val) {
int lo = 0, hi = arr.length;
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (arr[mid] >= val) hi = mid;
else lo = mid + 1;
}
return lo;
}
}

  

Approach #4: Mergesort.

class Solution(object):
def __init__(self):
self.cnt = 0 def reversePairs(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
def msort(lst):
L = len(lst)
if L <= 1:
return lst
else:
return merge(msort(lst[:int(L/2)]), msort(lst[int(L/2):])) def merge(left, right):
l, r = 0, 0
while l < len(left) and r < len(right):
if left[l] <= 2*right[r]:
l += 1
else:
self.cnt += len(left)-l
r += 1
return sorted(left+right) msort(nums)
return self.cnt

  

最新文章

  1. R语言向量
  2. Qt使用自带的windeployqt 生成exe来发布软件
  3. [转]十步完全理解SQL
  4. Mysql通信协议
  5. 编写简单的C/S聊天程序
  6. 高通Android平台硬件调试之Camera篇
  7. StrHelper
  8. CUDA编程-(2)其实写个矩阵相乘并不是那么难
  9. wpf 中DataGrid 控件的样式设置及使用
  10. haproxy简单负载均衡搭建
  11. Toy Storage POJ 2398
  12. JAVA高并发程序设计笔记
  13. [Alibaba-ARouter] 简单好用的Android页面路由框架
  14. R语言 重命名目录下所有文件
  15. Python:向MySQL数据库插文件
  16. maven 设置跳过测试
  17. 第一篇 Python图片处理模块PIL(pillow)
  18. eclipse设置条件断点
  19. Windows驱动手动卸载与安装
  20. .NET AOP微型框架发布 --CleanAOP

热门文章

  1. Java for LeetCode 093 Restore IP Addresses
  2. selenium WebDriverException: Message: unknown error: DevToolsActivePort file doesnt exist
  3. mysql错误:[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated
  4. IDEA:Application Server was not connected before run configuration stop, reason: Unable to ping 1099
  5. Hadoop- MapReduce在实际应用中常见的调优
  6. Ajax不能接受php return值的原因
  7. 分享知识-快乐自己:SpringMvc整合遇到-前台传JSON参数,后台实体类对象接收
  8. IDEA 设置忽略那些文件不提交到SVN服务器
  9. 通过在classpath自动扫描方式把组件纳入spring容器中管理。
  10. 时尚与深度学习系列:Fashion forward: Forecasting visual style in fashion