LeetCode-493. Reverse Pairs

目录

题目描述

解题思路

【C++】

【Java】


https://leetcode.com/problems/reverse-pairs/description/https://leetcode.com/problems/reverse-pairs/description/

题目描述

Given an integer array nums, return the number of reverse pairs in the array.

A reverse pair is a pair (i, j) where:

  • 0 <= i < j < nums.length and
  • nums[i] > 2 * nums[j].

Example 1:

复制代码
Input: nums = [1,3,2,3,1]
Output: 2
Explanation: The reverse pairs are:
(1, 4) --> nums[1] = 3, nums[4] = 1, 3 > 2 * 1
(3, 4) --> nums[3] = 3, nums[4] = 1, 3 > 2 * 1

Example 2:

复制代码
Input: nums = [2,4,3,5,1]
Output: 3
Explanation: The reverse pairs are:
(1, 4) --> nums[1] = 4, nums[4] = 1, 4 > 2 * 1
(2, 4) --> nums[2] = 3, nums[4] = 1, 3 > 2 * 1
(3, 4) --> nums[3] = 5, nums[4] = 1, 5 > 2 * 1

Constraints:

  • 1 <= nums.length <= 5 * 104
  • -231 <= nums[i] <= 231 - 1

解题思路

【C++】

cpp 复制代码
class Solution {
private:
    vector<int> indexes;
    vector<int> tmpIdxes;
    int ret = 0;

    void merge(vector<int>& nums, int start, int mid, int end) {
        int p1 = start, p2 = mid + 1, cur = start;
        while (p1 <= mid && p2 <= end) {
            while (p2 <= end && (long long) nums[indexes[p1]] <= (long long) nums[indexes[p2]] * 2) {p2++;}
            ret += end - p2 + 1;
            p1++;
        }
        p1 = start, p2 = mid + 1;
        while (p1 <= mid && p2 <= end) {tmpIdxes[cur++] = nums[indexes[p1]] > nums[indexes[p2]] ? indexes[p1++] : indexes[p2++];}
        while (p1 <= mid) {tmpIdxes[cur++] = indexes[p1++];}
        while (p2 <= end) {tmpIdxes[cur++] = indexes[p2++];}
        for (int i = start; i <= end; i++) {indexes[i] = tmpIdxes[i];}
    }

    void mergeSort(vector<int>& nums, int start, int end) {
        if (start < end) {
            int mid = start + (end - start) / 2;
            mergeSort(nums, start, mid);
            mergeSort(nums, mid + 1, end);
            merge(nums, start, mid, end);
        }
    }
public:
    int reversePairs(vector<int>& nums) {
        if (nums.size() < 2) {return 0;}
        indexes.resize(nums.size());
        tmpIdxes.resize(nums.size());
        for (int i = 0; i < nums.size(); i++) {indexes[i] = i;}
        ret = 0;
        mergeSort(nums, 0, nums.size() - 1);
        return ret;
    }
};

【Java】

java 复制代码
class Solution {
    private int[] index;
    private int[] tmpIndex;
    private int ans;
 
    private void merge(int[] nums, int start, int mid, int end) {
        int p1 = start, p2 = mid + 1, cur = start;
        while (p1 <= mid && p2 <= end) {
            while (p2 <= end && nums[index[p1]] <= (long) nums[index[p2]] * 2) {p2++;}
            ans += end - p2 + 1;
            p1++;
        }
        p1 = start; p2 = mid + 1;
        while (p1 <= mid && p2 <= end) {
            if (nums[index[p1]] > nums[index[p2]]) {tmpIndex[cur++] = index[p1++];}
            else {tmpIndex[cur++] = index[p2++];}
        }
        while (p1 <= mid) {tmpIndex[cur++] = index[p1++];}
        while (p2 <= end) {tmpIndex[cur++] = index[p2++];}
        for (int i = start; i <= end; i++) {index[i] = tmpIndex[i];}
    }
 
    private void mergeSort(int[] nums, int start, int end) {
        if (start < end) {
            int mid = start + (end - start) / 2;
            mergeSort(nums, start, mid);
            mergeSort(nums, mid + 1, end);
            merge(nums, start, mid, end);
        }
    }

    public int reversePairs(int[] nums) {
        index = new int[nums.length];
        tmpIndex = new int[nums.length];
        ans = 0;
        for (int i = 0; i < nums.length; i++) {index[i] = i;}
        mergeSort(nums, 0, nums.length - 1);
        return ans;
    }
}
相关推荐
Flower#1 小时前
D. Apple Tree Traversing 【Codeforces Round 1023 (Div. 2)】
c++·算法·图论·dfs
zhangfeng11332 小时前
Matlab 遗传算法的库 gads
算法·数据分析
究极无敌暴龙战神X2 小时前
hot100-子串-JS
javascript·数据结构·算法
codists8 小时前
《算法导论(第4版)》阅读笔记:p14-p16
算法
zilpher_wang9 小时前
K-means
算法·机器学习·kmeans
柃歌9 小时前
【LeetCode Solutions】LeetCode 176 ~ 180 题解
数据结构·数据库·sql·算法·leetcode
袁气满满~_~9 小时前
LeetCode:101、对称二叉树
算法·leetcode·职场和发展
How_doyou_do9 小时前
Dijkstra
算法
赵和范9 小时前
C++:书架
开发语言·c++·算法
tmiger10 小时前
图像匹配导航定位技术 第 10 章
人工智能·算法·计算机视觉