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. ftl总结

    当前项目前端是用freemarker,是第一次使用这种页面,一般语法不介绍,这里只是记录工作中遇到的问题 ---------2016.6.25-------------- 1.关于ftl字符串的问题 ...

  2. [Android] Gradle 安装

    Gradle安装非常简单,只要从官网下载压缩包,解压,修改一下环境变量即可. 笔者写本篇随笔时,版本是1.12. Windows下安装 1 到官网(http://www.gradle.org/down ...

  3. IOS 状态栏 显示与隐藏网络活动状态

    IOS中显示和隐藏状态栏的网络活动标志 //在向服务端发送请求状态栏显示网络活动标志: [[UIApplication sharedApplication] setNetworkActivityInd ...

  4. BZOJ1415 聪聪和可可 —— 期望 记忆化搜索

    题目链接:https://vjudge.net/problem/HYSBZ-1415 1415: [Noi2005]聪聪和可可 Time Limit: 10 Sec  Memory Limit: 16 ...

  5. Android SDK离线安装方法详解(加速安装)

    AndroidSDK在国内下载一直很慢··有时候通宵都下不了一点点,最后只有选择离线安装,现在发出离线安装地址和方法,希望对大家有帮助 一,首先下载SDK的安装包,android-sdk_r10-wi ...

  6. JavaScript(4)

    myfuns.js //自定义函数 //输入两个数,再输入一个运算符(+,-,*,/),得到结果->函数 function jiSuan(num1,num2,operator){//特别强调 参 ...

  7. Log4Net的使用之winform

    当我们将asp程序部署到远程服务器上的时候,如果遇到程序错误,如何能快速的判断我们程序的错误呢.所以-->Log4Net作为记录日志的一大神器,不得不学会熟练使用啊!没有那么多的原理,照猫画虎的 ...

  8. 团队作业第5周——测试与发布(Alpha版本)

    1.发现的bug a.同时按下和蛇前进方向相反的键和垂直方向的任意一个键贪吃蛇会死亡(比如贪吃蛇向右行走,同时按左上或左下都会game over) b.刷新的苹果会在蛇身上出现 暂时还没能力修复,以后 ...

  9. POJ1195Mobile phones (从二维树状数组到cdq分治)

    Suppose that the fourth generation mobile phone base stations in the Tampere area operate as follows ...

  10. Qt容器组件(一)之QGroupBox、QScrollArea、QToolBox、QTabWidget

    QT中有九种容器组件,分别是组合框QGroupBox.滚动区QScrollArea.工具箱QToolBox.选项卡QTabWidget.控件栈QWidgetStack.框架QFrame.组件QWidg ...