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

  

493. Reverse Pairs(BST, BIT, MergeSort)的更多相关文章

  1. [LeetCode] 493. Reverse Pairs 翻转对

    Given an array nums, we call (i, j) an important reverse pair if i < j and nums[i] > 2*nums[j] ...

  2. leetcode 493 Reverse Pairs

    题意:给定一个数组nums,求若 i<j and nums[i] > 2*nums[j] 的逆序对. Note: 数组的长度不会超过50,000 不愧是hard模式的题目,虽然已经知道可以 ...

  3. 第二周 Leetcode 493. Reverse Pairs(HARD)

    leetcode 493跟经典的逆序对问题没有什么区别, 首先考虑对数组前半部和后半部求逆序对数,若能保证两段数组都有序,则显然可以在线性时间内求出对数. 所以我们采用归并排序的方法,一方面让数组有序 ...

  4. 【leetcode】493. Reverse Pairs

    题目如下: 解题思路:本题要求的是数组每个元素和所有排在这个元素后面的元素的值的二倍做比较.我们可以先把数组所有元素的二倍都算出来,存入一个新的数组newlist,并按升序排好.而后遍历nums数组的 ...

  5. 493. Reverse Pairs

    // see more at https://www.youtube.com/watch?v=j68OXAMlTM4 // https://leetcode.com/problems/reverse- ...

  6. 493 Reverse Pairs 翻转对

    给定一个数组 nums ,如果 i < j 且 nums[i] > 2*nums[j] 我们就将 (i, j) 称作一个重要翻转对.你需要返回给定数组中的重要翻转对的数量.示例 1:输入: ...

  7. LeetCode -Reverse Pairs

    my solution: class Solution { public: int reversePairs(vector<int>& nums) { int length=num ...

  8. Reverse Pairs

    For an array A, if i < j, and A [i] > A [j], called (A [i], A [j]) is a reverse pair.return to ...

  9. [LintCode] Reverse Pairs 翻转对

    For an array A, if i < j, and A [i] > A [j], called (A [i], A [j]) is a reverse pair.return to ...

随机推荐

  1. ubuntu中设置wireshark抓包

    安装wireshark软件后,打开进行抓包的时候会提示权限不足.原因是普通用户没有执行权限,也打不开网络端口捕捉,因为dumpcap需要root权限. 产生这种问题的原因:比如:wireshark在进 ...

  2. C++三种继承方式

    一.三种继承方式 继承方式不同,第一个不同是的是派生类继承基类后,各成员属性发生变化.第二个不同是派生类的对象能访问基类中哪些成员发生变化.表格中红色标注.

  3. python获取本机IP地址

    方法一 通常使用socket.gethostname()方法即可获取本机IP地址,但有时候获取不到(比如没有正确设置主机名称) import socket #获取计算机名称hostname=socke ...

  4. mongo简介

    MongoDB MongoDB是一款强大.灵活.且易于扩展的通用型数据库 MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统. 在高负载的情况下,添加更多的节点,可以保证服 ...

  5. python之virtualenv 与 virtualenvwrapper 详解

    在使用 Python 开发的过程中,工程一多,难免会碰到不同的工程依赖不同版本的库的问题: 亦或者是在开发过程中不想让物理环境里充斥各种各样的库,引发未来的依赖灾难. 此时,我们需要对于不同的工程使用 ...

  6. ZOJ - 1504 Slots of Fun 【数学】

    题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1504 题意 给出一串字符串 里面的每个字符的位置 都按照题目的意 ...

  7. (linux)BSP(板上支持包)概述

    1. BSP概述 BSP即Board Support Package,板级支持包. 它来源于嵌入式操作系统与硬件无关的设计思想,操作系统被设计为运行在虚拟的硬件平台上. 对于具体的硬件平台,与硬件相关 ...

  8. 使用valgrind进行内存泄漏和非法内存操作检测

    valgrind是一个强大的工具,最常用的功能是用它来检测内存泄漏和非法内存的使用.要想让valgrind报告的更加细致,请使用-g进行编译. 基本命令如下: $ valgrind --tool=me ...

  9. Matlab的publish功能和cell功能

    Matlab的publish功能能够让写的代码变成优美的文档.类似为知笔记的markdown语言. cell功能配合publish使用,可以形成不同的功能块.而且调试的时候,可以按section调试. ...

  10. iOS 如何查看app提交审核是否使用广告标识符(IDFA)

    相信很多人被苹果拒绝过 : 您的 App 正在使用广告标识符 (IDFA).您必须先提供关于 IDFA 的使用信息或将其从 App 中移除,然后再上传您的二进制文件. 那么如何查看app里哪里用到ID ...