493 Reverse Pairs
我們在進行歸併排序的時候,會對兩個有序數組進行比較。
如果a1 比 b4 大, 那麼b1,b2,b3 都會比a1小。
利用這個邏輯,我們就可以嘗試一次批量獲取所有比a1大的數。
並且,如果我們能夠確認a數組中所有的數字再原數組中的下標比b數組中要小,那麼我們就等於可以批量獲取逆序對的數量了。
如何實現?
我們只需要在普通歸併排序的模板中,合併比較的時候,把左邊數組比右邊數組大的條件加一句
ans += right - rightStartingIndex;
代碼如下:
#include <iostream>
using namespace std;
int ans = 0;
int temp[1000];
int line[1000];
void merge_sort(int left, int right){if(left == right)return;int mid = (left + right) / 2;merge_sort(left, mid);merge_sort(mid + 1, right);for(int i = left; i <= right; i++){temp[i] = line[i];}int i1 = left, int i2 = mid + 1;for(int curr = left; curr <= right; curr++){if(il == mid + 1){line[curr] = temp[i2++];}else if(i2 > right){line[curr] = temp[i1++];}else if(temp[i1] <= temp[i2]){line[curr] = temp[i1++];}else{ans += (mid - i1 + 1);line[curr] = temp[i2++];}}return ;
}
Leetcode 493
雖然同時需要找逆序對,可是這一題把逆序對的定義變成num[i] > 2*num[j], where i < j,
我們只需要在進入歸併前,進行數數就可以了。
記住我們並不可以向上面的代碼那樣數數。
比如這個情況
1 2 3 1 1 2 3
1 2 3 1 | 1 2 3
for 1 2 3 1
1 2 | 3 1 ans += 1
for 1 2 3 : skip
for 1 1 2 3 | 1 2 3
由於2 比 1大 ,1就直接被消掉,然後後面來了3,確沒了1,那就算少了,所以最方便還是把數數放在外面。
class Solution {
private:int ans = 0;int temp[60000];vector<int> line;
public:int reversePairs(vector<int>& nums) {line = nums;merge_sort(0, nums.size() - 1);return ans;}void merge_sort(int left, int right){if(left == right)return;int mid = (left + right) / 2;merge_sort(left, mid);merge_sort(mid + 1, right);int g = mid + 1;for(int k = left; k <= mid; k++){while(g <= right && long(line[k]) > 2*long(line[g])){g++;}ans += g - mid - 1;}for(int i = left; i <= right; i++){temp[i] = line[i];}int i1 = left, i2 = mid + 1;for(int curr = left; curr <= right; curr++){if(i1 == mid + 1) line[curr] = temp[i2++];else if(i2 > right) line[curr] = temp[i1++];else if(temp[i1] <= temp[i2]) line[curr] = temp[i1++];else line[curr] = temp[i2++];}return;}
};
493 Reverse Pairs相关推荐
- [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 t ...
- Reverse Pairs
For an array A, if i < j, and A [i] > A [j], called (A [i], A [j]) is a reverse pair. return t ...
- [Swift]LeetCode493. 翻转对 | Reverse Pairs
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...
- leetcode练习 Reverse Pairs
几次练手之后,决定进军hard难度 最近的课程主要讲的是分治,就选择了相关的题目. Given an array nums, we call (i, j) an important reverse p ...
- Merge Sort及其对一类问题的应用
1.归并排序 O(nlogn) stable #include <iostream> #include <vector> using namespace std;void me ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
原文地址:https://www.cnblogs.com/grandyang/p/4606334.html 终于将LeetCode的大部分题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开 ...
- taoqick 搜索自己CSDN博客
L1 L2正则化和优化器的weight_decay参数 kaiming初始化的推导 Pytorch动态计算图 Pytorch自动微分机制 PyTorch中在反向传播前为什么要手动将梯度清零? 通俗讲解 ...
- pytorch做seq2seq注意力模型的翻译
以下是对pytorch 1.0版本 的seq2seq+注意力模型做法语--英语翻译的理解(这个代码在pytorch0.4上也可以正常跑): 1 #-*- coding: utf-8 -*- 2 &qu ...
- Pytorch系列教程-使用Seq2Seq网络和注意力机制进行机器翻译
NLP FROM SCRATCH: TRANSLATION WITH A SEQUENCE TO SEQUENCE NETWORK AND ATTENTION Author: Sean Roberts ...
最新文章
- 全国青少年软件编程python等级考试-全国青少年软件编程等级考试:Python一级考试大纲...
- mssql自定义函数中使用游标
- 关于Java中try-catch-finally-return语句的思考
- tpshop防止sql注入补丁
- java EL表达式中${param.name}详细
- 理解流 java 0325
- linux 内存泄露检测工具——valgrind
- 翁恺老师C语言学习笔记(九)数组的运算
- 【网络安全工程师面试合集】—邮件协议是如何被安全人员利用的?
- 为何手机厂商如此热衷 5G?
- 加密和解密盐的使用_码农吐糟面试官:居然问我md5是对称加密还是非对称,故意的吧?...
- 【C# RDLC】 RDLC报表的部署问题
- 吴恩达新书《Machine Learning Yearning》完整中文版PDF
- linux主备dns切换时间,linux下主从DNS配置相关知识(二)
- mysql 自动加上编号
- Ubuntu 离线安装软件包
- 图像处理之双线性插值法
- html实心圆圈,html5使用canvas画空心圆与实心圆
- Verilog学习之路(1)— Quartus II 13.0下载安装和HelloWorld
- 配置路由器console接口密码和vty接口密码
热门文章
- 如何解决安卓手机显示google play服务停止运行?
- pcl点云三维重建面片赋予彩色
- 深入浅出零钱兑换问题——背包问题的套壳
- 读书笔记之-Analyzing Neural Time Series Data 1、2
- python长表转换宽表_Pandas数据变换-长宽表互换
- [JAVA学习笔记] 21天学习挑战赛之流程控制语句
- 启动容器时报错:iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 1217 -j DNAT --to-de
- 解决微信小程序rich-text 长文本不换行问题解决
- 【django】DRF
- JavaScript网页全屏API