493 Reverse Pairs 翻转对

给定一个数组 nums ,如果 i < j 且 nums[i] > 2*nums[j] 我们就将 (i, j) 称作一个重要翻转对。
你需要返回给定数组中的重要翻转对的数量。
示例 1:
输入: [1,3,2,3,1]
输出: 2
示例 2:
输入: [2,4,3,5,1]
输出: 3
注意:
    给定数组的长度不会超过50000。
    输入数组中的所有数字都在32位整数的表示范围内。
详见:https://leetcode.com/problems/reverse-pairs/description/

C++:

class Solution {
public:
    int reversePairs(vector<int>& nums) {
        int n = nums.size();
        if(n <= 1)
        {
            return 0;
        }
        int cnt = 0;
        vector<int> b(nums.begin(), nums.begin() + n / 2);
        vector<int> c(nums.begin() + n / 2, nums.end());
        cnt += reversePairs(b);
        cnt += reversePairs(c);
        int ai = 0, bi = 0, ci = 0;
        while(ai < n)
        {
            if(bi < b.size() && (ci == c.size() || b[bi] <= c[ci]))
            {
                nums[ai++] = b[bi++];
            }
            else
            {
                long tmp2 = (long)c[ci]*2;
                int low = 0,high = b.size() - 1,pos = bi;
                while(low <= high)
                {
                    pos = (low + high)/2;
                    if((long)b[pos] == tmp2)
                    {
                        low++;
                    }
                    else if((long)b[pos] > tmp2)
                    {
                        high = pos - 1;
                    }
                    else
                    {
                        low = pos + 1;
                    }
                }
                if(low < b.size() && low >= 0 && (long)b[low] > tmp2)
                {
                    cnt += n/2 - low;
                }
                nums[ai++] = c[ci++];
            }
        }
        return cnt;
    }
};

参考:https://blog.csdn.net/lin360580306/article/details/60326795

原文地址:https://www.cnblogs.com/xidian2014/p/8904156.html

时间: 2024-07-31 13:40:25

493 Reverse Pairs 翻转对的相关文章

[LeetCode] Reverse Pairs 翻转对

Given an array nums, we call (i, j) an important reverse pair if i < j and nums[i] > 2*nums[j]. You need to return the number of important reverse pairs in the given array. Example1: Input: [1,3,2,3,1] Output: 2 Example2: Input: [2,4,3,5,1] Output:

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]. You need to return the number of important reverse pairs in the given array. Example1: Input: [1,3,2,3,1] Output: 2 Example2: Input: [2,4,3,5,1] Output:

LeetCode 493. Reverse Pairs

原题链接在这里:https://leetcode.com/problems/reverse-pairs/ 题目: Given an array nums, we call (i, j) an important reverse pair if i < j and nums[i] > 2*nums[j]. You need to return the number of important reverse pairs in the given array. Example1: Input: [1

493. Reverse Pairs

Given an array nums, we call (i, j) an important reverse pair if i < j and nums[i] > 2*nums[j]. You need to return the number of important reverse pairs in the given array. Example1: Input: [1,3,2,3,1] Output: 2 Example2: Input: [2,4,3,5,1] Output:

【leetcode】493. Reverse Pairs

好吧这其实是一次小比赛,在回上海前的周末.好久没打比赛,简单题也只是AK了而已,还TM错了一次.最后45名57分钟加一次罚时,第一名18分钟,好吧.也行了对于业余组选手. 错在没考虑到乘2会超max_int. 程序没了,也没心情找了.孤单的情人节. 貌似有两个人被发现作弊了我变成了43名,还找到了程序,也算一点小小的安慰吧. class Solution { public: int reversePairs(vector<int>& aa) { vector<long long&

[Swift]LeetCode493. 翻转对 | Reverse Pairs

Given an array nums, we call (i, j) an important reverse pair if i < j and nums[i] > 2*nums[j]. You need to return the number of important reverse pairs in the given array. Example1: Input: [1,3,2,3,1] Output: 2  Example2: Input: [2,4,3,5,1] Output:

LeetCode -Reverse Pairs

my solution: class Solution { public: int reversePairs(vector<int>& nums) { int length=nums.size(); int count=0; for (int i=0;i<length;i++) { for(int j=i+1;j<length;j++) { if(nums[i]>2*nums[j]) count++; } } return count; } }; wrong answ

Reverse Pairs

For an array A, if i < j, and A [i] > A [j], called (A [i], A [j]) is a reverse pair.return total of reverse pairs in A. Example Given A = [2, 4, 1, 3, 5] , (2, 1), (4, 1), (4, 3) are reverse pairs. return 3 Solution 1: the same as the leetcode ques

[LeetCode] 190. Reverse Bits 翻转二进制位

Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000). Follow up:If this function