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;
    }
}
相关推荐
代码游侠18 小时前
日历的各种C语言实现方法
c语言·开发语言·学习·算法
春日见1 天前
丝滑快速拓展随机树 S-RRT(Smoothly RRT)算法核心原理与完整流程
人工智能·算法·机器学习·路径规划算法·s-rrt
Code小翊1 天前
”回调“高级
算法·青少年编程
云里雾里!1 天前
力扣 977. 有序数组的平方:双指针法的优雅解法
算法·leetcode·职场和发展
一只侯子1 天前
Face AE Tuning
图像处理·笔记·学习·算法·计算机视觉
jianqiang.xue1 天前
别把 Scratch 当 “动画玩具”!图形化编程是算法思维的最佳启蒙
人工智能·算法·青少年编程·机器人·少儿编程
不许哈哈哈1 天前
Python数据结构
数据结构·算法·排序算法
J***79391 天前
后端在分布式系统中的数据分片
算法·哈希算法
天真小巫1 天前
2025.11.28总结
职场和发展
Dream it possible!1 天前
LeetCode 面试经典 150_二叉搜索树_二叉搜索树中第 K 小的元素(86_230_C++_中等)
c++·leetcode·面试