C++ solution using STL lower_bound


  • 0
    N

    class Solution {
    public:
    int reversePairs(vector<int>& nums) {
    vector<double> t;
    int i;
    int res=0;
    for (i=nums.size()-1;i>=0;i--) {
    if (t.empty()){
    t.push_back(nums[i]);
    }
    else {
    int d = distance(t.begin(), lower_bound(t.begin(), t.end(),nums[i]/2.0) );
    res += d;
    int d1 = distance(t.begin(), lower_bound(t.begin(), t.end(), nums[i]));
    t.insert(t.begin()+d1, nums[i]);
    }

        }
        return res;
    }
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.