493. Reverse Pairs
// see more at https://www.youtube.com/watch?v=j68OXAMlTM4
// https://leetcode.com/problems/reverse-pairs/discuss/97268/general-principles-behind-problems-similar-to-reverse-pairs
// http://www.cnblogs.com/grandyang/p/6657956.html class Solution {
public:
int reversePairs(vector<int>& nums) {
return reversePairs(nums, , nums.size()-);
}
int reversePairs(vector<int>& nums, int begin, int end) {
if (begin >= end) return ;
int m = begin + (end - begin) / ;
int res = reversePairs(nums, begin, m);
res += reversePairs(nums, m+, end); int *p = new int[end-begin+]; int i = begin, j = m+;
while (i <= m && j <= end) {
if (nums[i] > 2L * nums[j]) { // avoid overflow
res += m - i + ;
j++;
}
else {
i++;
}
} i = begin; j = m+; int k = ;
while (i <= m && j <= end) {
if (nums[i] > nums[j])
p[k++] = nums[j++];
else
p[k++] = nums[i++];
}
while (i <= m)
p[k++] = nums[i++];
while (j <= end)
p[k++] = nums[j++];
for (int i = begin; i <= end; i++) {
nums[i] = p[i-begin];
}
//sort(nums.begin() + begin, nums.begin() + end+1); return res;
}
};
493. 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] ...
- 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 493. Reverse Pairs(HARD)
leetcode 493跟经典的逆序对问题没有什么区别, 首先考虑对数组前半部和后半部求逆序对数,若能保证两段数组都有序,则显然可以在线性时间内求出对数. 所以我们采用归并排序的方法,一方面让数组有序 ...
- 【leetcode】493. Reverse Pairs
题目如下: 解题思路:本题要求的是数组每个元素和所有排在这个元素后面的元素的值的二倍做比较.我们可以先把数组所有元素的二倍都算出来,存入一个新的数组newlist,并按升序排好.而后遍历nums数组的 ...
- leetcode 493 Reverse Pairs
题意:给定一个数组nums,求若 i<j and nums[i] > 2*nums[j] 的逆序对. Note: 数组的长度不会超过50,000 不愧是hard模式的题目,虽然已经知道可以 ...
- 493 Reverse Pairs 翻转对
给定一个数组 nums ,如果 i < j 且 nums[i] > 2*nums[j] 我们就将 (i, j) 称作一个重要翻转对.你需要返回给定数组中的重要翻转对的数量.示例 1:输入: ...
- [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 ...
- 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 翻转对
Reverse Pairs 翻转对 题意 计算数组里面下标i小于j,但是i的值要大于j的值的两倍的搭配的个数(也就是可能会有多种搭配):网址 做法 这道题显然是不允许使用最简单的方法:两次循环,逐次进 ...
随机推荐
- jQuery核心探究
动画animate.animate({}, { duration: 5000, progress: function (now, progressNum) {}, complete: function ...
- 转:Windows任务计划实现自动执行ArcGIS相关功能
今天一不小心点开了Windows任务计划,以前咩有怎么用过,发现还挺好用,于是想到了以前用户的一些问题 1:用户环境使用ArcSDE服务连接,每次运行到一定的负载量(可能是几天),就会很慢,用户就喜欢 ...
- Conferences
中国学术会议在线 :http://www.meeting.edu.cn 中国会议网: http://www.chinameeting.com 香山科学会议:http://www.xssc.ac.cn/ ...
- 解决dubbo-admin管控台不能显示服务的问题
1.首先在网上下载了dubbo-admin.war,解压后修改dubbo.properties文件 dubbo.registry.address=zookeeper://127.0.0.1:2181 ...
- 动软代码生成器,主子表增加的时候子表的parentID无法插入问题解决方案
StringBuilder strSql=new StringBuilder(); strSql.Append("insert into HT_XunJiaMain("); str ...
- Windows server R2 2008上部署gogs git
所需的环境 1. 安装mysql 安装路径:F:\MySQL Server 5.7 2. 安装gogs ...
- MySQL子查询有哪五种形式?
MySQL是一个关系型数据库管理系统,由瑞典MySQLAB公司开发,目前属于Oracle旗下产品.MySQL是最流行的关系型数据库管理系统之一,在web应用方面,MySQL是最好的RDBMS(Rela ...
- U深度U盘启动盘制作工具怎么用?U深度U盘启动盘制作工具使用教学
U深度u盘启动盘制作工具是一款强大的启动盘制作软件,对于新手用户来说,由于软件专业度很高,想一下就上手是比较困难的.所以这里给大家分享一篇U深度u盘启动盘制作工具的使用教程. 使用教程: 第一步:安装 ...
- "COM Surrogate 已停止工作"解决方案(windows7 64位及32位)
根据图示步骤,将以下文件添加至“数据执行保护”的例外列表中. 64位:C:Windows\SysWOW64\dllhost.exe 32位:C:\Windows\System32\dllhost.ex ...
- 如何使用事务码SMICM分析ABAP代码发起的HTTP请求的错误ICM_HTTP_SSL_PEER_CERT_UNTRUSTED
当我用CL_HTTP_CLIENT往一个外网的url发请求时,遇到错误:ICM_HTTP_SSL_PEER_CERT_UNTRUSTED 错误是从这段ABAP代码里抛出来的: CALL METHOD ...