逆序对问题

最近刷leetcode 315. Count of Smaller Numbers After Self,遇到问题所以想趁机总结下这类问题。

逆序对定义
在数组中的两个数字,如果前面一个数字大于后面一个数字,则这两个数字组成一个逆序对。

递归问题求解逆序对
就是将数组分为两部分,左边数组中有left个逆序对,右边数组中有right逆序对,还有一种这个逆序对存在左右两个数组之间。

public class InversePairs {

    public int InversePairs(int [] array) {
        int len = array.length;
        if(len == 0 || len == 1) return 0;
        return getCount(array, 0, len-1);

    }
    public int getCount(int []array, int begin, int end) {
        if(begin >= end) return 0;
        int mid = begin +(end-begin) /2;
        //逆序对存在左右两边的逆序对,加上交叉的逆序对
        int lCount = getCount(array, begin, mid);
        int rCount = getCount(array, mid+1, end);
        int[] tmp = new int[end-begin +1];
        int tmpIdx = end-begin;
        int startIdx = mid;
        int endIdx = end;
        int crossCount = 0;

        //以下是正常递归的过程的merge过程
        while(startIdx >= begin && endIdx >= mid+1) {
            if(array[startIdx] > array[endIdx]) { //存在逆序数
                crossCount += (endIdx - mid);  // 读这个大数而言, 存在endIdx - mid逆序对,该大数就处理完了,可以放入到临时数组中
                if (crossCount > 1000000007)// 数值过大求余
                {
                    crossCount %= 1000000007;
                }

                tmp[tmpIdx] = array[startIdx];
                tmpIdx --;
                startIdx --;
            }else {
                tmp[tmpIdx] = array[endIdx];
                tmpIdx --;
                endIdx --;

            }


        }

        while(startIdx >= begin) {
            tmp[tmpIdx] = array[startIdx];
            tmpIdx --;
            startIdx --;
        }
        while(endIdx >= mid+1) {
            tmp[tmpIdx] = array[endIdx];
            tmpIdx --;
            endIdx --;
        }

        // 将临时数组的值copy回源数组

        for(int i=0;i<=tmp.length-1;i++) {
            array[begin+i] = tmp[i];    
        }

        return (lCount + rCount + crossCount) % 1000000007;
    }



    public static void main(String[] args) {
        InversePairs s = new InversePairs();
        int [] array = new int[] {1,2,3,4,5,6,7,0};
        System.out.println(s.InversePairs(array));
    }

这到题可以在牛客网上测试是否全部用例都通过,注意
if (crossCount > 1000000007)// 数值过大求余
{
crossCount %= 1000000007;
}

这段不写的话 不能通过,排查了半天才发现原因是这个。

同理可以解决leetcode上的题, 记录的时候记录每一个位置上的逆序对个数,而不是总的逆序对。

“`
int [] count;
public List countSmaller(int[] nums) {
List res = new ArrayList();
count = new int[nums.length];
int[] indexes = new int[nums.length];

    // 按顺序排序的时候,需要记录当前数字的下标
    for(int i=0;i<nums.length;i++) {
        indexes[i] = i;
    }
    mergesort(nums, indexes, 0, nums.length-1);  //归并排序
    for(int i=0;i<count.length; i++) {
        res.add(count[i]);
    }
    return res;

}
private void mergesort(int[] nums, int[] indexes, int start, int end) {
    if( end <= start) {
        return;
    }
    int mid = (start + end) /2;
    mergesort(nums, indexes, start, mid); // 左边已经排序完毕
    mergesort(nums, indexes, mid+1, end); // 右边已经排序完毕
    merge(nums, indexes, start,end); // merge过程中计算的cross的
}

private void merge(int[] nums, int[] indexes, int start, int end) {
    int mid = (start + end) /2;
    int left_index = start;
    int right_index = mid+1;
    int rightcount = 0;
    int [] new_indexes = new int[end-start + 1];
    int sort_index = 0;
    while(left_index <= mid && right_index <=end) {
        if(nums[indexes[right_index]] < nums[indexes[left_index]]) {
            new_indexes[sort_index]  = indexes[right_index];
            rightcount ++;
            right_index ++;
        }else {
            new_indexes[sort_index] = indexes[left_index];
            count[indexes[left_index]] += rightcount;
            left_index ++;
        }
        sort_index ++;

    }

    while(left_index <= mid) {
        new_indexes[sort_index] = indexes[left_index];
        count[indexes[left_index]] += rightcount;
        left_index ++;
        sort_index ++;
    }
    while(right_index <= end) {
        new_indexes[sort_index++] = indexes[right_index++];
    }
    for(int i=start; i<= end;i++){
        indexes[i] = new_indexes[i-start];
    }


}```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值