[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 total of reverse pairs in A.
Example
Given A = [2, 4, 1, 3, 5] , (2, 1), (4, 1), (4, 3) are reverse pairs. return 3
这道题跟LeetCode上的那道Count of Smaller Numbers After Self是一样的,唯一的一点点的小区别是那道题是返回一个向量,表示出原数组中每一个数字的右边比其小的数的个数,而这道题让我们求翻转对的总数,其实就是把每个数字右边比其小的数的个数都加起来即可,具体讲解请参加之前那篇博客Count of Smaller Numbers After Self,参见代码如下;
class Solution {
public:
long long reversePairs(vector<int>& A) {
long long res = ;
vector<int> v;
for (int i = A.size() - ; i >= ; --i) {
int left = , right = v.size();
while (left < right) {
int mid = left + (right - left) / ;
if (A[i] > v[mid]) left = mid + ;
else right = mid;
}
v.insert(v.begin() + right, A[i]);
res += right;
}
return res;
}
};
[LintCode] Reverse Pairs 翻转对的更多相关文章
- [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] ...
- [LeetCode] Reverse Pairs 翻转对
Reverse Pairs 翻转对 题意 计算数组里面下标i小于j,但是i的值要大于j的值的两倍的搭配的个数(也就是可能会有多种搭配):网址 做法 这道题显然是不允许使用最简单的方法:两次循环,逐次进 ...
- [LintCode] Reverse Integer 翻转整数
Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-bit integer). ...
- 493 Reverse Pairs 翻转对
给定一个数组 nums ,如果 i < j 且 nums[i] > 2*nums[j] 我们就将 (i, j) 称作一个重要翻转对.你需要返回给定数组中的重要翻转对的数量.示例 1:输入: ...
- [Swift]LeetCode493. 翻转对 | Reverse Pairs
Given an array nums, we call (i, j) an important reverse pair if i < j and nums[i] > 2*nums[j] ...
- 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 ...
- LeetCode -Reverse Pairs
my solution: class Solution { public: int reversePairs(vector<int>& nums) { int length=num ...
- 493. Reverse Pairs(BST, BIT, MergeSort)
Given an array nums, we call (i, j) an important reverse pair if i < j and nums[i] > 2*nums[j] ...
- [LeetCode] 190. Reverse Bits 翻转二进制位
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
随机推荐
- hpunix下11gRac的安装
一.检查环境 1.操作系统版本# uname -a 2.补丁包三大补丁包#swlist -l bundle|grep QPKAPPS#swlist -l bundle|grep QPKBASE#swl ...
- Unity3d 提示 "The scripts file name does not match the name of the class defined in the script!"的解决办法
有两个原因,一个是文件的名称和类名不一致 第二个原因是有命名空间, 排除应该是可以修复的
- java杂记——数组拷贝
这里介绍两种java提供的数组拷贝方法: (1)Arrays提供的copyOf(T src, T desLength)和copyOfRange(T src, int from, int to) (2) ...
- Streaming data from Oracle using Oracle GoldenGate and Kafka Connect
This is a guest blog from Robin Moffatt. Robin Moffatt is Head of R&D (Europe) at Rittman Mead, ...
- Jmeter之csv、用户自定义变量以及Query Type分析(八)
很多童鞋不知道对于Jmeter的Query Type 不知道选哪个,为什么选,怎么选! 下面这边做个简单的分析, 那么首先什么是CSV Data Set Config,有什么用呢? CSV Data ...
- CodeForces 710F 强制在线AC自动机
题目链接:http://codeforces.com/contest/710/problem/F 题意:维护一个集合,集合要求满足三种操作. 1 str:向集合插入字符串str(保证不会插入之前已经插 ...
- 简单的实现UIpicker上面的取消确定按钮
1 因为我用的xib实现的添加picker 和textfiled的, @interface ViewController : UIViewController<UITextFieldDelega ...
- http://www.cnblogs.com/summers/p/3225375.html
http://www.cnblogs.com/summers/p/3225375.html
- HDU5727 Necklace(枚举 + 二分图最大匹配)
题目大概说有n个yang珠子n个yin珠子,要交替串成一个环形项链,有些yang珠子和某个yin珠子相邻这个yang珠子会不高兴,问最少有几个yang珠子不高兴. 自然会想到直接用状压DP去解,转移很 ...
- Excel: Switch (transpose) columns and rows
链接:https://support.office.com/en-in/article/Switch-transpose-columns-and-rows-ed1215f5-59af-47e6-953 ...