[leetcode]493. Reverse Pairs

匿名 (未验证) 提交于 2019-12-03 00:26:01

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

nums(i, j)important reverse pairi < jnums[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: 3

class Solution { public:     int reversePairs(vector<int>& nums) {         return sort_and_count(nums.begin(),nums.end());     }          int sort_and_count(vector<int>::iterator begin,vector<int>::iterator end)     {         if(end-begin<=1)             return 0;         auto mid=begin+(end-begin)/2;         int count=sort_and_count(begin,mid)+sort_and_count(mid,end);                  for(auto i=begin,j=mid;i!=mid;i++)         {             while(j!=end && *i>2L * (*j))                 j++;             count+=j-mid;         }         inplace_merge(begin,mid,end);         return count;     } };

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!