当前位置:网站首页>【Leetcode】493. Reverse Pairs
【Leetcode】493. Reverse Pairs
2022-07-26 04:58:00 【记录算法题解】
题目地址:
https://leetcode.com/problems/reverse-pairs/
给定一个长 n n n的数组 A A A,求 ∣ { ( i , j ) : i < j ∧ A [ i ] > 2 A [ j ] } ∣ |\{(i,j):i<j\land A[i]>2A[j]\}| ∣{(i,j):i<j∧A[i]>2A[j]}∣。
可以用树状数组。先将 A A A连同 2 A 2A 2A一起做保序离散化(用哈希表),接着可以用经典求法。遍历 A A A,对于 A [ i ] A[i] A[i],我们求一下 A [ 0 : i − 1 ] A[0:i-1] A[0:i−1]里满足小于等于 2 A [ i ] 2A[i] 2A[i]的数有多少个,可以在树状数组里求前缀和,如果有 t t t个,则大于 2 A [ i ] 2A[i] 2A[i]的就有 i − t i-t i−t个;然后再将 A [ i ] A[i] A[i]离散化之后,将这个位置的数加 1 1 1即可。代码如下:
class Solution {
public:
#define lowbit(x) (x & -x)
vector<int> tr;
int n;
void add(int k, int x) {
for (; k <= n; k += lowbit(k)) tr[k] += x;
}
int sum(int k) {
int res = 0;
for (; k; k -= lowbit(k)) res += tr[k];
return res;
}
int reversePairs(vector<int>& A) {
vector<long> tmp(A.begin(), A.end());
for (auto &x : A) tmp.push_back(2L * x);
sort(tmp.begin(), tmp.end());
n = unique(tmp.begin(), tmp.end()) - tmp.begin();
unordered_map<long, int> mp;
for (int i = 0; i < n; i++) mp[tmp[i]] = i + 1;
tr = vector<int>(n + 1);
int res = 0;
for (int i = 0, x; i < A.size(); i++) {
x = A[i];
res += i - sum(mp[x * 2L]);
add(mp[x], 1);
}
return res;
}
};
时间复杂度 O ( n log n ) O(n\log n) O(nlogn),空间 O ( n ) O(n) O(n)。
Java:
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class Solution {
int[] tr;
int n;
int lowbit(int x) {
return x & -x;
}
void add(int k, int x) {
for (; k <= n; k += lowbit(k)) {
tr[k] += x;
}
}
int sum(int k) {
int res = 0;
for (; k > 0; k -= lowbit(k)) {
res += tr[k];
}
return res;
}
public int reversePairs(int[] A) {
long[] tmp = new long[A.length << 1];
for (int i = 0; i < A.length; i++) {
tmp[2 * i] = A[i];
tmp[2 * i + 1] = A[i] * 2L;
}
Arrays.sort(tmp);
int m = 0;
for (int i = 0; i < tmp.length; i++) {
if (m == 0 || tmp[i] > tmp[m - 1]) {
tmp[m++] = tmp[i];
}
}
Map<Long, Integer> map = new HashMap<>();
for (int i = 0; i < m; i++) {
map.put(tmp[i], i + 1);
}
n = m;
tr = new int[m + 1];
int res = 0;
for (int i = 0, x; i < A.length; i++) {
x = A[i];
res += i - sum(map.get(x * 2L));
add(map.get((long) x), 1);
}
return res;
}
}
时空复杂度一样。
边栏推荐
- Tonight! Stonedb is officially open source. Please check this strategy~
- 你对“happen-before原则”的理解可能是错的?
- [wp][gwctf 2019] boring lottery
- [mathematical modeling] analytic hierarchy process (AHP)
- can 串口 can 232 can 485 串口转CANbus总线网关模块CAN232/485MB转换器CANCOM
- What is the real HTAP? (1) Background article
- UE4 displays text when it is close to the object, and disappears when it is far away
- 计算离散点的曲率(matlab)
- Sliding window -- leetcode solution
- autocomplete禁止表单自动填充
猜你喜欢

Embedded practice -- CPU utilization statistics based on rt1170 FreeRTOS (24)

UE4 two ways to obtain player control

What is the difference between asynchronous and synchronous transmission signals (electronic hardware)

Is this my vs not connected to the database

时代潮流-云原生数据库的崛起

图像非局部均值滤波的原理

Redis解决库存超卖问题

ES6 modularization +commonjs

Yapi installation

2022 Henan Mengxin League game (3): Henan University L - synthetic game
随机推荐
2022河南萌新联赛第(三)场:河南大学 A - 玉米大炮
There was an unexpected error (type=Method Not Allowed, status=405).记录报错
常函数const的学习
uniapp小程序框架-一套代码,多段覆盖
2022 Henan Mengxin League game (3): Henan University L - synthetic game
What points should be paid attention to in the selection of project management system?
To study the trend of open source and gain insight into the future of the industry, stonedb community and the China Academy of communications and communications released the Research Report on the dev
AXI协议(4):AXI通道上的信号
C language -- string function, memory function collection and Simulation Implementation
Array sort 1
What are the restrictions on opening futures accounts? Where is the safest place to open an account?
data warehouse
Building blocks for domestic databases, stonedb integrated real-time HTAP database is officially open source!
快恢复二极管工作原理及使用
UE4 displays text when it is close to the object, and disappears when it is far away
UE4 controls the rotation of objects by pressing keys
域名解析过程全分析,就着文字理解更佳
9、 File upload and download
奥特学园ROS笔记--6
Google Emoji guessing game helps parents guide their children to surf the Internet safely