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 question 315. Count of Smaller Numbers After Self

class Solution {
public:
    /**
     * @param A an array
     * @return total of reverse pairs
     */
    long long reversePairs(vector<int>& A) {
        // Write your code here
        long long res=0;
        vector<int> t;
        for (int i=A.size()-1;i>=0;i--){
            int index=lower_bound(t.begin(),t.end(),A[i])-t.begin();
            t.insert(t.begin()+index,A[i]);
            res+=index;
        }
        return res;
    }
};

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Verdana; color: #075db3; background-color: #fefef2 }
span.s1 { }

时间: 2024-11-03 21:49:59

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:

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

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:

[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 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&

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

lintcode:逆序对

题目 在数组中的两个数字如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.给你一个数组,求出这个数组中逆序对的总数.概括:如果a[i] > a[j] 且 i < j, a[i] 和 a[j] 构成一个逆序对. 样例 序列 [2, 4, 1, 3, 5] 中,有 3 个逆序对 (2, 1), (4, 1), (4, 3),则返回 3 . 解题 直接暴力找,时间复杂度O(n^2) public class Solution { /** * @param A an array * @ret